Java基础之流程控制

一、顺序结构

       顺序结构的程序语句只能被执行一次。如果您想要同样的操作执行多次,,就需要使用循环结构。

  if-else-if 语句

语法:

  if(条件){

         当条件为true时,执行大括号内的代码

  }else if(条件){}

代码实例:

public static void main(String[] args){
       int a=2;
       if(a>1){
             System.out.println("该数字大于1");
       }else if(a<1){
                   System.out.println("该数字小于1");
             }
        System.out.println("该数字为1");
}

 switch语句

     switch 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。

     语法:

     switch( 变量 ){

     case 值1:

            break;

     case 值2:

            break;

      default:

       }

      当程序执行到break关键字时,跳出当前的switch语句;

代码实例:

 public static void main(String[] args){
            int b=2;
            switch(b){
                case 4:System.out.println("该值是4");   //情况一
                    break;
                case 2:System.out.println("该值是2");   //情况二
                    break;
                default:
            }

        }

       注意事项:

  • switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串类型了,同时 case 标签必须为字符串常量或字面量。
  • switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。
  • case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。
  • 当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。
  • 当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。
  • switch 语句可以包含一个 default 分支,该分支必须是 switch 语句的最后一个分支。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。

二、循环结构

  while循环语句

  语法:

  while(条件){

         当条件为true时,执行循环体内的代码;

  }

  备注:满足循环的三个条件:初始化变量、关系运算、迭代

public static void main(String[] args){
           while(true){   //只要条件为true程序就一直会执行下去
               System.out.println("我爱你");
           }
       }

 do-while循环语句

  语法:

  do{

  }while(条件);

  备注:与while循环的区别是,当条件为false时,也会被执行一次。

public static void main(String[] args){
           do {
               System.out.println("我爱你");
           } while(false);
       }
  for循环语句

  语法:

  for(int i = 0 ; i < 10 ; i++){

  }

       嵌套for循环

打印直角三角形

for(int i = 1 ; i < 10 ; i++){
            for(int j = 1 ; j <= i ; j++){
                System.out.print("* ");
            }
            System.out.println();

输出其结果

* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * * * 
* * * * * * * * * 

打印乘法口诀

for(int i = 1 ; i < 10 ; i++){
            for(int j = 1 ; j < 10-i ; j++){
                System.out.print("	");
            }
            for(int j = 1 ; j <=i ; j++){
                System.out.print(j+"×"+i+"="+(i*j)+"	");
            }
            System.out.println();
        }

输出结果

                                                                               1×1=1    
                                                                      1×2=2    2×2=4    
                                                             1×3=3    2×3=6    3×3=9    
                                                   1×4=4    2×4=8    3×4=12    4×4=16    
                                        1×5=5    2×5=10    3×5=15    4×5=20    5×5=25    
                              1×6=6    2×6=12    3×6=18    4×6=24    5×6=30    6×6=36    
                    1×7=7    2×7=14    3×7=21    4×7=28    5×7=35    6×7=42    7×7=49    
         1×8=8     2×8=16    3×8=24    4×8=32    5×8=40    6×8=48    7×8=56    8×8=64    
1×9=9    2×9=18    3×9=27    4×9=36    5×9=45    6×9=54    7×9=63    8×9=72    9×9=81   
  增强 for 循环

       Java5 引入了一种主要用于数组的增强型 for 循环。

       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("
");
      String [] names ={"James", "Larry", "Tom", "Lacy"};
      for( String name : names ) {
         System.out.print( name );
         System.out.print(",");
      }
   }
}

运行结果

10,20,30,40,50,
James,Larry,Tom,Lacy,

三、循环控制

  break语句

  break 可以用于所有的循环语句或者 switch 语句中,用来跳出整个语句块。

  break 跳出该关键字所在的循环,并且继续执行该循环下面的语句。

代码实例

public class Test {
   public static void main(String args[]) {
      int [] numbers = {10, 20, 30, 40, 50};
 
      for(int x : numbers ) {
         // x 等于 30 时跳出循环
         if( x == 30 ) {
            break;
         }
         System.out.print( x );
         System.out.print("
");
      }
   }
}

运行结果

10
20

  continue语句

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

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

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

运行实例

public class Test {
   public static void main(String args[]) {
      int [] numbers = {10, 20, 30, 40, 50};
 
      for(int x : numbers ) {
         if( x == 30 ) {
        continue;
         }
         System.out.print( x );
         System.out.print("
");
      }
   }
}

运行结果

10
20
40
50

标签的使用

语法:

标签名 :

使用:

break 标签名;  或

continue 标签名;

a: for (int i = 1; i <= 10; i++) {
            System.out.println("i="+i);
            b: for (int j = 1; j <= 10; j++) {
                if(j==5){
//                    continue a;
                    break a;
                }
                System.out.println("j="+j);
            }
            
        }

注意:

标签名的语法规则要遵循标识符的语法要求;

break 标签名 : 跳出该标签名后面的循环;

continue 标签名: 跳过该标签名的本次循环,继续进行下次迭代;



 

 

原文地址:https://www.cnblogs.com/shenzhenhuaya/p/10739067.html