IT Band of Brothers Grammar Java flow control statements branching structure sentence 3

3 if-else if-else conditional statements

if-else if-else intact form as follows:

if (determination condition A) {

    A statement block (A determination condition is true, execution)

} Else if (the determination condition B) {

    B statement block (B determination condition is true, execution)

}else{

    Statement block C (A value determination condition and the determination condition B are both false, execution)

}…

Of course, if only one statement statement block, then the code may use the following simplified form:

if (determination condition A) A block of statements;

else if (the determination condition B) B block of statements;

else C statement block;

Conditional expression is from top to bottom check the individual once the true condition is found, associated with the implementation of the statement, and the statement following will be skipped, if the conditional expression is not true, on the implementation last else statements. The last else is often used as a default condition, that is, if all other conditions are not true, on the implementation of the final else. Without the final else, other conditions are false, the branched structure will skip the entire block of statements, execution continues back code. Execution flow if-else if-else statement is shown in FIG.

11ed8a5d7b71478c88489d2995fd1fd5.png

FIG execution flow 5 if-else if-else statements

 

The following program will demonstrate the implementation process if-else if-else conditional statement:

public class IfElseIfElseDemo{

    public static void main(String[] args){

         int a = 4;

         if(a > 4){

             System.out.println ( "a greater than 4");

         }else if(a == 4){

             System.out.println ( "a equals 4");

         }else{

             System.out.println ( "a less than 4");

         }

    }

}

Compile and run this program, the console displays a message shown in Figure 6.

058527fa9564490cb2076fac68eaf774.png

FIG 6 IfElseIfElseDemo operating results

 

It can be seen by the results, if the entire branching structure, the second expression is true, the code block is executed after the second if, then exit if the entire branched structure, the subsequent execution codes.

Guess you like

Origin www.cnblogs.com/itxdl/p/11261980.html