program flow control statement
if
if( boolean expression) {
// Statement that will be executed if the boolean expression is true
}
If the boolean expression evaluates to true, the code block within the if statement is executed, otherwise the code following the if statement block is executed.
Code demo:
public static void main(String[] args) { int a = 10; if(a>8){ System.out.println("real"); } }
Compilation result: true
if else
if( boolean expression) else {
//if boolean expression is true
}else {
//if boolean expression is false
}
If the boolean expression is true, execute the code of the if statement block, if the boolean expression is false, execute the code of the else statement block
Code demo:
int a = 10; if(a>10){ System.out.println("real"); }else{ System.out.println("Fake"); }
Compilation result: false
if...else if...else
if( boolean expression 1)
{ //Execute code if boolean expression 1 evaluates to true }
else if( boolean expression 2)
{ //Execute code if boolean expression 2 evaluates to true }
else if( boolean expression 3)
{ //Execute code if boolean expression 3 evaluates to true }
else { //Execute code if none of the above boolean expressions are true }
Suitable for detecting a variety of possible situations, more suitable for judging branch intervals
Note the implementation:
- Statements have at most one else statement, and the else statement follows all else if statements.
- Statements can have several else if statements, which must precede the else statement.
- Once one of the else if statements is detected as true, the execution of the other else if and else statements will be skipped.
Code demo:
public static void main(String[] args) { int score = 10; if(score>=90 && score<=100){ System.out.println("excellent"); }else if(score>=80){ System.out.println("excellent"); }else if(score>=70){ System.out.println("good"); }else if(score>=60){ System.out.println("good"); }else if(score>-1){ System.out.println("failed"); }else{ System.out.println("The score is unreasonable"); } }
Compilation result: Failed
switch case
switch(expression){
case value : // statement
break; // optional
case value : // statement
break; // optional
// you can have any number of case statements
default : // optional
// statement
}
The switch case statement has the following rules:
- The variable type in the switch statement can be: byte, short, int, or char. Since Java SE 7, switch supports the String type, and the case label must be a string constant or literal (or the corresponding wrapper type, and also supports enumeration).
- A switch statement can have multiple case statements. Each case is followed by a value to compare and a colon.
- The data type of the value in the case statement must be the same as the data type of the variable, and can only be a constant or literal constant.
- When the value of the variable is equal to the value of the case statement, the statement after the case statement begins to execute, and the switch statement is not jumped out until the break statement occurs.
- The switch statement terminates when a break statement is encountered. The program jumps to the execution of the statement following the switch statement. A case statement does not have to contain a break statement. If no break statement occurs, the program continues with the next case statement until a break statement occurs.
- A switch statement can contain a default branch, which is generally the last branch of the switch statement (it can be anywhere, but the last one is recommended). default is executed when the value of no case statement is equal to the variable value. The default branch does not require a break statement.
final char a = 'a';//If the compilation is not modified with final, an error will be reported, only constants int m = (int)(Math.random()*26)+97;//Randomly generate a numerical value of 97-123; char c = (char)m;//Coerce numeric to character switch(c){ case a : case 'e': case 'i': case 'o': case 'u':System.out.println(c+": is a vowel"); break; default: System.out.println(c+": is a consonant"); }
Compilation result:
If there is no break statement in the case statement block, the JVM will not sequentially output the return value corresponding to each case, but will continue to match. If the match is unsuccessful, the default case will be returned.
Code demo:
final char a = 'a';//If the compilation is not modified with final, an error will be reported, only constants switch(a){ case a :System.out.println("a");//Run through the execution after encountering a match a case 'e':System.out.println("e"); case 'i':System.out.println("i"); case 'o':System.out.println("o"); case 'u':System.out.println("u"); }
Compilation result:
a
e
i
o
u