5、Java入门教程【循环+条件语句】

一、循环

java有三种主要的循环结构:

  • while 循环
  • do…while 循环
  • for 循环

1、while 循环

//语法
while( 布尔表达式 ) {
    
    
  //循环内容
}

//示例
public class Test {
    
    
   public static void main(String[] args) {
    
    
      int x = 10;
      while( x < 20 ) {
    
    
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }
}

2、do…while 循环

//语法
do {
    
    
       //代码语句
}while(布尔表达式);

//示例
public class Test {
    
    
   public static void main(String[] args){
    
    
      int x = 10;
 
      do{
    
    
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }while( x < 20 );
   }
}

3、for循环

//语法
for(初始化; 布尔表达式; 更新) {
    
    
    //代码语句
}

//示例
public class Test {
    
    
   public static void main(String[] args) {
    
    
 
      for(int x = 10; x < 20; x = x+1) {
    
    
         System.out.print("value of x : " + x );
         System.out.print("\n");
      }
   }
}

4、Java 增强 for 循环

//语法
for(声明语句 : 表达式)
{
    
    
   //代码句子
}

//示例
public class Test {
    
    
   public static void main(String[] args){
    
    
      int [] numbers = {
    
    10, 20, 30, 40, 50};
 
      for(int x : numbers ){
    
    
         System.out.print( x );
         System.out.print(",");
      }
      System.out.print("\n");
      String [] names ={
    
    "James", "Larry", "Tom", "Lacy"};
      for( String name : names ) {
    
    
         System.out.print( name );
         System.out.print(",");
      }
   }
}

5、break 关键字

break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。

break 跳出最里层的循环,并且继续执行该循环下面的语句。

6、continue 关键字

continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。

在 for 循环中,continue 语句使程序立即跳转到更新语句。

在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。

二、if条件语句

1、if…else语句

//语法
if(布尔表达式){
    
    
   //如果布尔表达式的值为true
}else{
    
    
   //如果布尔表达式的值为false
}

//示例
public class Test {
    
    
 
   public static void main(String args[]){
    
    
      int x = 30;
 
      if( x < 20 ){
    
    
         System.out.print("这是 if 语句");
      }else{
    
    
         System.out.print("这是 else 语句");
      }
   }
}

2、if…else if…else 语句

//语法
if(布尔表达式 1){
    
    
   //如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
    
    
   //如果布尔表达式 2的值为true执行代码
}else if(布尔表达式 3){
    
    
   //如果布尔表达式 3的值为true执行代码
}else {
    
    
   //如果以上布尔表达式都不为true执行代码
}

//示例
public class Test {
    
    
   public static void main(String args[]){
    
    
      int x = 30;
 
      if( x == 10 ){
    
    
         System.out.print("Value of X is 10");
      }else if( x == 20 ){
    
    
         System.out.print("Value of X is 20");
      }else if( x == 30 ){
    
    
         System.out.print("Value of X is 30");
      }else{
    
    
         System.out.print("这是 else 语句");
      }
   }
}

三、switch条件语句

switch(expression){
    
    
    case value :
       //语句
       break; //可选
    case value :
       //语句
       break; //可选
    //你可以有任意数量的case语句
    default : //可选
       //语句
}
  • switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断

  • switch(变量)中,变量不支持 longdouble 类型的数据匹配

switch可以实现的逻辑,if都可以实现。if还支持区间匹配。

猜你喜欢

转载自blog.csdn.net/bobo789456123/article/details/131796006
今日推荐