Latest web development tutorials

Java branch structure - if ... else / switch

Sequence structure only the order of execution can not be judgments and choices, and therefore require a branched structure.

Java has two branch structure:

  • if statement
  • switch statement

if statement

If a statement contains a boolean expression and one or more statements.

grammar

If the statement with the following syntax:

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

If the Boolean expression evaluates to true, the if statement block of code. Otherwise If statement block behind the code execution.

public class Test {

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

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

The above code is compiled results are as follows:

这是 if 语句

if ... else statement

Later if statement with else statements when the boolean expression of the if statement is false, else statement block is executed.

grammar

if ... else used as follows:

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

Examples

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 is compiled results are as follows:

这是 else 语句

if ... else if ... else statement

Later if statement with elseif ... else statement, this statement can detect a variety of possible scenarios.

Use if, else if, else statement, the need for attention to the following points:

  • if the statement at most one else statements before all else statements elseif statements.
  • If statements can have several elseif statements, they must be before the else statement.
  • Once one else if statement is detected as true, other else if and else statements are skipped executed.

grammar

if ... else syntax is as follows:

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

Examples

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 above code is compiled results are as follows:

Value of X is 30

Nested if ... else statement

Nested if-else statement is legitimate. This means that you can use if or elseif statement in another if or elseif statement.

grammar

Nested if ... else syntax is as follows:

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

You can be like the if statement nested else if ... else.

Examples

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 is compiled results are as follows:

X = 30 and Y = 10

switch statement

switch statement to determine a value of a variable with a range of values ​​are equal, and each value is called a branch.

grammar

switch syntax is as follows:

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

switch statement has the following rules:

  • switch statement only variable type is byte, short, int or char.
  • switch statement can have multiple case statements. Behind each case followed by a colon and the value to be compared.
  • case statement data type of value must be the same as the data type of the variable, and can only be a constant or literal.
  • When the value of a variable equal to the value of case statement, the statement after the case statement is executed until the break statement would appear out of the switch statement. 3
  • When it encounters a break statement, switch statement is terminated. The program jumps to the statement following the switch statement is executed. case statement does not have to contain a break statement. If there is no break statement appears, the program will continue with the next case statement until the break statement.
  • switch statement can contain a default branch that must be the last branch of the switch statement. default execution in no case statements and variable values ​​equal time. default branch do not need to break the statement.

Examples

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 above code is compiled results are as follows:

Well done
Your grade is C