关闭

Control Statements of Java -from The Complete Reference Java

485人阅读 评论(0) 收藏 举报

Java'a program control statements can be put into three categories:

1.selection statements : if and switch

    if(condition) statement1;
    else statement2;

IF:
    if-else
    nested ifs
    the if-else-if ladder

Switch
Nested switch statements               
    switch(expression){
         case value1:
             //statement sequence
             break;
         case value2:
              //statement sequence
              break;
         ....
         case valueN:
                  //statement sequence
          break;
        default:
              //default statement sequence
    }

The expression must be of type byte,short,int or char, each of the values specified in the case statements
 must be of a type compatible with the expression. Each case value must be a unique literal( that is, it must
be a constant, not a variable). Duplicate case values are not allowed.

    The switch statement works like this: the value of expression is compared with each of the literal values in the case statements. If a match is found, the code sequence following that case statement is executed. If 
none of the constants matches the value of the expression, then the default statement is executed. However, the default statement is optional. If no case matches and no default is present, then no further action is taken.

The break statement is used inside the switch to terminate a statement sequence. This has the effect of "junping out" of the switch.

Java complier works:
    When it complies a switch statement, the Java complier will inspect each of the case constants and create a
"jump table" that it will use for selecting the path of execution depending on the value of the expression. Therefore, if you need to select among a large group of values, a switch statement will run much faster than the equivalent logic coded using a sequence of if-elses. The complier can do this bacause it knows that the case constants are all the same type and simply must be compared for equality with switch expression. The complier has no such knowledge of a long list of if expression.
   

2.iteration statements

        2.1.while

        2.2.do while

        2.3.for

3.jump

        3.1break

        3.1continue

        3.1return

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8103次
    • 积分:191
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档