Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java Branch Structure - if... else/switch


May 10, 2021 Java


Table of contents


Java Branch Structure - if... else/switch

Sequential structures can only be executed sequentially and cannot be judged and selected, so branch structures are 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 used in the If statement is as follows:

if(布尔表达式)
{
   //如果布尔表达式为true将执行的语句
}

If the value of the Boolean expression is true, the block of code in the if statement is executed. O therwise, the code behind 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 above code compiles and runs as follows:

这是 if 语句

if... Else statement

The if statement can be followed by the else statement, and when the boolean expression value of the if statement is false, the else statement block is executed.

Grammar

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

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

Instance

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 above code compiles and runs as follows:

这是 else 语句

if... e lse if... Else statement

If statement can be followed by else if... else statement, which can detect a variety of possible scenarios.

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

  • The if statement has up to 1 else statement, and the else statement is after all the else if statements.
  • An If statement can have several else if statements, which must be before the else statement.
  • Once one of the else if statements is detected as true, the other else if and else statements skip execution.

Grammar

if... T he else syntax format is as follows:

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

Instance

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("这是 else 语句");
      }
   }
}

The above code compiles and runs as follows:

Value of X is 30

Nested if... Else statement

It is legal to use nested if-else statements. T his means that you can use an if or else if statement in another if or else if statement.

Grammar

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

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

You can nest else if like an if statement... else。

Instance

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 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 types in the switch statement can only be byte, short, int, or char. Starting with Java SE 7, switch supports string String types, and case labels must be string constants or literals.
  • A switch statement can have more than one case statement. Each case is followed by a value and colon to compare.
  • 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 constant or literal constant.
  • When the value of a variable is equal to the value of a case statement, the statement after the case statement begins to execute and does not jump out of the switch statement until the break statement appears.
  • When a break statement is encountered, the switch statement terminates. T he program jumps to the statement execution that follows the switch statement. T he case statement does not have to contain a break statement. If no break statement appears, the program continues 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. D efault is executed when there is no case statement with an equal value for the variable. The default branch does not require a break statement.

Instance

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("优秀"); 
            break;
         case 'B' :
         case 'C' :
            System.out.println("良好");
            break;
         case 'D' :
            System.out.println("及格");
         case 'F' :
            System.out.println("你需要继续努力");
            break;
         default :
            System.out.println("无效等级");
      }
      System.out.println("你的等级是 " + grade);
   }
}

The above code compiles and runs as follows:

良好
你的等级是 C