Java branch structure


The sequential structure can only be executed sequentially, and judgment and selection cannot be made, so a branch structure is required.

Java has two branch structures:

  •             if statement

  •           switch statement


if statement

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

Grammar

The syntax of the If statement is as follows:

if(布尔表达式)
{
   //如果布尔表达式为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.

public class Test {

   public static void main(String args[]){
      int x = 10;

      if( x < 20 ){
         System.out.print("这是 if 语句");
      }
   }
}

The result of compiling and running the above code is as follows:

这是 if 语句

if...else statement

The if statement can be followed by an else statement, when the Boolean expression of the if statement When the expression value is false, the else statement block will be executed.

Grammar

The usage of if...else is as follows:

if(布尔表达式){
   //如果布尔表达式的值为true
}else{
   //如果布尔表达式的值为false
}

Example

public class Test {

   public static void main(String args[]){
      int x = 30;

      if( x < 20 ){
         System.out.print("这是 if 语句");
      }else{
         System.out.print("这是 else 语句");
      }
   }
}

The compilation and running results of the above code are as follows:

这是 else 语句

if...else if...else statement

The if statement can be followed by an elseif...else statement, which can detect a variety of possible situations.

When using if, else if, else statements, you need to pay attention to the following points:

  • ##               The if statement has at most one else statement, and the else statement comes after all elseif statements.

  •             If statement can have several elseif statements, they must precede else statement.

  •             Once one of the else if statements detects true, the execution of the other else if and else statements will be skipped.

Grammar

if...else syntax format is as follows:

if(布尔表达式 1){
   //如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
   //如果布尔表达式 2的值为true执行代码
}else if(布尔表达式 3){
   //如果布尔表达式 3的值为true执行代码
}else {
   //如果以上布尔表达式都不为true执行代码
}

Example

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 else statement");
      }
   }
}

The compilation and running results of the above code are as follows:

Value of X is 30


Nested if...else statements

It is legal to use nested if-else statements. This means you can use an if or elseif statement within another if or elseif statement.

Syntax

The nested if...else syntax format is as follows:

if(布尔表达式 1){
   ////如果布尔表达式 1的值为true执行代码
   if(布尔表达式 2){
      ////如果布尔表达式 2的值为true执行代码
   }
}

You can nest

else if just like if statements. ..else.

Example

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 compilation and running results of the above code are as follows:

X = 30 and Y = 10


switch statement

The switch statement determines whether a variable matches a certain value in a series of values Whether the values ​​are equal, each value is called a branch.

Grammar

The switch syntax format is as follows:

switch(expression){
    case value :
       //语句
       break; //可选
    case value :
       //语句
       break; //可选
    //你可以有任意数量的case语句
    default : //可选
       //语句
}

The switch statement has the following rules:

  •                    The variable type in the switch statement can only be byte, short, int or char.

  •             A switch statement can have multiple case statements. Each case is followed by a value to be compared 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 it can only be a constant or literal constant.

  •           When the value of the variable is equal to the value of the case statement, the statements after the case statement begin to execute, and the switch statement will not jump out until the break statement appears. 3

  •           The switch statement terminates when a break statement is encountered. The program jumps to the statement following the switch statement for execution. The case statement does not have to contain a break statement. If no break statement appears, the program will continue executing the next case statement until a break statement appears.

  •           A switch statement can contain a default branch, which must be the last branch of the switch statement. 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.

Example

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("Well done");
            break;
         case 'D' :
            System.out.println("You passed");
         case 'F' :
            System.out.println("Better try again");
            break;
         default :
            System.out.println("Invalid grade");
      }
      System.out.println("Your grade is " + grade);
   }
}

The results of compiling and running the above code are as follows:

Well done
Your grade is C