Java branch structure - if...else/switch

The sequential structure can only be executed sequentially and cannot be judged and selected. Therefore, the branch structure is required.
Java has two branches:
  • If statement
  • Switch statement

If statement

An if statement contains a Boolean expression and one or more statements.

grammar

The syntax for the if statement is as follows:
if ( boolean expression ) { // statement to be executed if boolean expression is true }
If the value of the boolean expression is true, the code block in the if statement is executed, otherwise the code following the if statement block is executed.

Test.java file code:

public class Test { public static void main(String args[]){ int x = 10; if( x < 20 ){ System.out.print("This is an if statement"); } } }
The above code compiles and runs as follows:
This is an if statement  

If...else statement

The if statement can be followed by an else statement. When the boolean expression of the if statement evaluates to false, the else statement block is executed.

grammar

The usage of if...else is as follows:
if ( boolean expression ) { // if the boolean expression evaluates to true } else { // if the Boolean expression is false }

Examples

Test.java file code:

public class Test {
public static void main ( String args [ ] ) {
int x = 30 ; if ( x < 20 ) {
System . out . print ( " This is an if statement " ) ; } else {
System . out . print ( " This is the else statement " ) ; } } }
The above code compiles and runs as follows:
This is the else statement  

if...else if...else 语句

The if statement can be followed by the elseif...else statement, which can detect many possible conditions.
When using the if, else if, else statements, note the following points:
  • The if statement has at most one else statement and the else statement follows all elseif statements.
  • The if statement can have several elseif statements that must precede the else statement.
  • Once one of the else if statements detects true, the other else if and else statements skip execution.

grammar

The if...else syntax is as follows:
if ( boolean expression 1 ) { // if the value of boolean expression 1 is true execute the code } else if ( boolean expression 2 ) { // If boolean expression 2 evaluates to true, code executes } else if ( boolean expression 3 ) { // If boolean expression 3 evaluates to true, code executes } else { // if none of the above Boolean expressions are true execute the code }

Examples

Test.java file code:

public class Test { public static void main(String args[]){ int x = 30; if( x == 10 ){ System.out.print("Value of X is 10"); }else if( x == 20 ){ System.out.print("Value of X is 20"); }else if( x == 30 ){ System.out.print("Value of X is 30"); }else{ System.out.print("This is an else statement"); } } }
The above code compiles and runs as follows:
Value of X is 30

Nested if...else statements

Using nested if...else statements is legal. This means that you can use if or elseif statements in another if or elseif statement.

grammar

The nested if...else syntax is as follows:
If ( boolean expression 1 ) { // // if the value of boolean expression 1 is true execute the code If ( boolean expression 2 ) { // // if the value of boolean expression 2 is true execute the code } }
You can nest else if...else like an if statement.

Examples

Test.java file code:

public class Test { public static void main(String args[]){ int x = 30; int y = 10; if( x == 30 ){ if( y == 10 ){ System.out.print("X = 30 and Y = 10"); } } } }
The above code compiles and runs as follows:
X = 30 and Y = 10

Switch statement

The switch statement determines whether a variable is equal to a value in a series of values, each of which is called a branch.

grammar

The switch syntax is as follows:
switch(expression){ case value : //Statements break ; // Optional Case value : // statements break ; // Optional // you can have any number of case statements default : // optional // statements }
The switch statement has the following rules:
  • The variable type in the switch statement can be: byte, short, int, or char. Since Java SE 7, the switch supports string types, and the case tag must be a string constant or literal.
  • A switch statement can have multiple case statements. Each case is followed by a value and a colon to be compared.
  • The data type of the value in the case statement must be the same as the data type of the variable, and it can only be a constant or a literal constant.
  • When the value of the variable is equal to the value of the case statement, then the statement after the case statement begins execution, and the switch statement will not jump out until the break statement appears.
  • When a break statement is encountered, the switch statement terminates. The program jumps to the statement following the switch statement. The case statement does not have to include a break statement. If no break statement occurs, the program will continue to execute the next case statement until the break statement appears.
  • The switch statement can contain a default branch, which must be the last branch of the switch statement. Default executes when there is no case statement whose value is equal to the value of the variable. The default branch does not require a break statement.

Examples

Test.java file code:

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("Excellent"); break; case 'B' : case 'C' : System.out.println("GOOD"); Break ; case ' D ' : System . out . println ( " pass " ) ; case ' F ' : System . out . println ( " You need to work hard again " ) ; break ; default : System . out . println ( " unknown " Level " ) ; } System . out . println ( " Your grade is " + grade ) ; } }
The above code compiles and runs as follows:
Good Your rating is C

Comments