Java flow control condition of the judge sentences

First, the judge sentences

  1, it is determined if a single statement sentence 1--

    Syntax:

if (relational expression) { 
  statements thereof; 
}

     Implementation process:

      •  First look at the structure to determine the relationship between the expression is true or false;
      •     If the statement is true on the implementation of the body;
      •     If the statement is false does not execute body.

    flow chart:

  

  2, the judge sentences 2-- if ... else statement

    Syntax:

if (relational expression) { 
  statement body. 1; 
} {the else 
  sentence 2; 
}

    Implementation process:

      •   First, determine the relationship between the expression to see the result is true or false; 
      •      If the statement is true on the implementation body 1; 
      •     If false statement on the implementation of the body 2;

      flow chart:

  

 

  3, the judge sentences 3 - if ... else if ... else statement

    Syntax:

if (judgment condition 1) { 
  execute statement 1; 
} the else if (judgment condition 2) { 
  execute statement 2; 
} 
... 
} the else if (n-determination condition) { 
  execute statement n-; 
} {the else 
  executing statements n + 1; 
}

    Implementation process:

      •    First, determine the relationship between the expression 1 to see the result is true or false;
      •       If the statement is true on the implementation body 1;
      •       If it is false to continue to determine the relationship between the expression 2 to see the result is true or false;
      •       If the statement is true on the implementation of the body 2;
      •       If it is false to continue to determine the relationship between the expression ... see the result is true or false;
      •       .....
      •       If there is no relationship between the expression is true, then the statement is executed body n + 1

   流程图: 

  

 

  4、if 语句和三元运算符的互换

    在某些简单的应用中, 可以使用 if...else 语句和三元运算符互换使用的。这样看起来更简便一些。

   Demo:

 1 public static void main(String[] args) {
 2   int a = 10;
 3   int b = 20;
 4   //定义变量,保存a和b的较大值
 5   int c;
 6   if(a > b) {
 7     c = a;
 8   } else {
 9     c = b;
10   }
11   //可以上述功能改写为三元运算符形式
12   c = a > b ? a:b;
13 }

 

 

二、选择语句

  1、选择语句 -- switch

    语句格式:

switch(表达式) {
case 常量值1:
  语句体1;
  break;
case 常量值2:
  语句体2;
  break;
...
default:
  语句体n+1;
  break;
}

    执行流程:

      •  首先计算出表达式的值
      •     其次,和case依次比较,一旦有对应的值,就会执行相应的语句,在执行的过程中,遇到break就会结 束。
      •     最后,如果所有的case都和表达式的值不匹配,就会执行default语句体部分,然后程序结束掉。

   流程图:

  

    注意:switch语句中,表达式的数据类型,可以是byte,short,int,char,enum(枚举),JDK7后可以接收字符串(String)。

  2、case 的穿透性

    在switch语句中,如果case的后面不写break,将出现穿透现象,也就是不会在判断下一个case的值,直接向后运 行,直到遇到break,或者整体switch结束。

   Demo:

 1 public static void main(String[] args) {
 2   int i = 5;
 3 switch (i){
 4   case 0:
 5     System.out.println("执行case0");
 6     break;
 7   case 5:
 8     System.out.println("执行case5");
 9   case 10:
10     System.out.println("执行case10");
11   default:
12     System.out.println("执行default");
13   }
14 }

     上述程序中,执行case5后,由于没有break语句,程序会一直向后走,不会在判断case,也不会理会break,直接 运行完整体switch。

     Tips:在使用 switch 语句时,写上  break 语句是非常有必要的。

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11231908.html