10、流程控制

1、流程控制
1.2、顺序结构

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

2、判断语句
2.1、if
  • if(布尔表达式) { 
         //如果布尔表达式为true将执行的语句 
    }
    
  • 如果布尔表达式的值为 true,则执行 if 语句中的代码块,否则执行 if 语句块后面的代码。

  • public class Test {
         public static void main(String args[]){
             int x = 10;
             if( x < 20 ){
                 System.out.print("这是 if 语句");
             }
         }
     }  
    
2.2、if…else
  • if 语句后面可以跟 else 语句,当 if 语句的布尔表达式值为 false 时,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.3、嵌套的if…else
  • if(布尔表达式 1) {
        //如果布尔表达式 1的值为true执行代码
        if(布尔表达式 2){
            //如果布尔表达式 2的值为true执行代码
        }
    }
    
2.4、if…else if…else
  • if(布尔表达式 1){
        //如果布尔表达式 1的值为true执行代码
    }else if(布尔表达式 2){
        //如果布尔表达式 2的值为true执行代码
    }else if(布尔表达式 3){
        //如果布尔表达式 3的值为true执行代码
    }else {
        //如果以上布尔表达式都不为true执行代码
    }
    
2.5、if语句和三元运算符的转换
  • public class DemoCycle {
         public static void main(String[] args) {
             int a = 10;
             int b = 20;
             //int max = a > b ? a : b;
             int max;
             if (a > b) {
                 max = a;
             } else {
                 max = b;
             }
             System.out.println("最大值:" + max);
         }
     }
    
3、选择语句
3.1、switch
  • switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。

  •     switch(expression){
         case value :
            //语句
            break; //可选
         case value :
            //语句
            break; //可选
         //你可以有任意数量的case语句
         default : //可选
            //语句
        }
    
3.1.1、注意
  • switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。

  • switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。

  • case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。

  • 当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。

  • 当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break
    语句。

  • switch 语句可以包含一个 default 分支,该分支一般是 switch 语句的最后一个分支(可以在任何位置,但建议在最后一个)。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要
    break 语句。

  • switch case 执行时,一定会先进行匹配,匹配成功返回当前case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。

  • 举个栗子

      public class Test {
          public static void main(String args[]){
            //char grade = args[0].charAt(0);
            char grade = 'C';
       
            switch(grade)
            {
               case 'A' :
                  System.out.println("优秀"); 
                  break;
               case 'B' :
               case 'C' :
                  System.out.println("良好");
                  break;
               case 'D' :
                  System.out.println("及格");
                  break;
               case 'F' :
                  System.out.println("你需要再努力努力");
                  break;
               default :
                  System.out.println("未知等级");
            }
            System.out.println("你的等级是 " + grade);
         }
      }
    
  • 如果case语句块中没有break语句时,JVM并不会顺序输出每一个case对应的返回值,而是继续匹配,匹配不成功则返回默认 case。

3.1、case的穿透性

看上一条

4、循环语句
4.1、什么是循环
4.2、for
  • for循环执行的次数是在执行前就确定的。

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

  • public class DemoCycle {
       public static void main(String[] args) {
           for (int i = 1; i <= 10; i++) {
               System.out.println(i);
           }
       }
    }
    
4.2.1、增强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(",");
               }
          }
     }
    
4.3、while
  • while是最基本的循环,它的结构为:

    • while( 布尔表达式 ){ //循环内容 }
  • 只要布尔表达式为 true,循环就会一直执行下去。

  • 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");
             } 
       }
    }
    
4.4、do…while
  • 对于 while 语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。

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

  • 布尔表达式在循环体的后面,所以语句块在检测布尔表达式之前已经执行了。 如果布尔表达式的值为 true,则语句块一直执行,直到布尔表达式的值为 false。

  • 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");
                }
           }
    }
    
4.5、循环语句的区别
4.6、跳出语句
4.6.1、break
  • break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。break 跳出最里层的循环,并且继续执行该循环下面的语句。
4.6.2、continue
  • continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。在 for 循环中,continue 语句使程序立即跳转到更新语句。在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。
5、扩展内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值