Java flow control of the select statement

Select Statement

Analyzing select statement statements also known, there are two kinds of writing, one is the if statement, one is the switch statement. Here we describe in detail the use of these two statements.

If the judge sentences

  • The first form of the if statement: if
format:
Implementation process
  • First, determine the relationship between the expression to see the result is true or false
  • If the statement is true on the implementation of the body
  • If false statement is not executed body 

Execution flow as shown in FIG.

 Code Example

package MyCode;

public  class Demo01If {
     public  static  void main (String [] args) {
         // 3 variables 
        int A =. 1 ;
         int B = 2 ;
         int C =. 1 ;
         // relational expression determining true or to false 
        IF (A == b) {
            System.out.println("a == b");
        }
        IF (A == C) {
             // braces the body of code, the implementation of relational expression is true 
            System.out.println ( "A == C" );
        }
        System.out.println ( "Other statements" );
    }
}

The result of code execution

  • The second form of the if statement: if ... else 
format:
Implementation process
  • First, determine the relationship between the expression to see the result is true or false
  • If the statement is true on the implementation of 1
  • If false statement on the implementation of 2 

Code execution flow as shown below

 Code Example

package MyCode;

public  class DemoIfElse {
     public  static  void main (String [] args) {
         // determines the given data is odd or even
         // define the variable 
        int I = 22 is ;
         // Analyzing relational expression to see the result is true or to false 
        IF ( 0% 2 == I ) {
             // to true if executed back braces code 
            System.out.println ( "remainder is 0, an even number" );
        } Else {
             // to false on the implementation of the code behind braces else 
            System.out.println ( "the remainder is not 0, an odd number" );
        }
    }
}

The result of code execution

  • The third form of the if statement: if ... else if ... else 
format:
Implementation process
  • First, determine the relationship between the expression 1 to see the result is true or false
  • If the statement is true on the implementation of the body 1, the relationship between expression and body behind the statement is not executed.
  • If it is false to continue to determine the relationship between the expression 2 to see the result is true or false
  • If the statement is true on the implementation of the body 2, the relationship between expression and body behind the statement is not executed.
  • If it is false to continue to determine the relationship between the expression ... see the result is true or false
  • If there is no relationship between the expression is true, then the statement is executed body n + 1. 

Code execution flow as shown below

Code Example

package MyCode;

public class DemoIfElseExt {
    /*
    Specifies the test scores to determine student grades
     90-100 Excellent
     80-89 Good
     70-79 good
     60-69 pass
      60 The following fail
     */
    public static void main(String[] args) {
        int score = 100;
        if (score < 0 || score > 100) {
            System.out.println ( "Your achievement is wrong" );
        } else if (score >= 90 && score <= 100) {
            System.out.println ( "Your achievements belong excellent" );
        } else if (score >= 80 && score < 90) {
            System.out.println ( "Your achievements belong to good" );
        } else if (score >= 70 && score < 80) {
            System.out.println ( "Your achievements belong to good" );
        } else if (score >= 60 && score < 70) {
            System.out.println ( "Your achievements belong to fail" );
        } else {
            System.out.println ( "Your achievements belong to fail" );
        }
    }
}

The result of code execution

if statement and the ternary operator exchange

In some simple applications, if the statement is a ternary operator can be used interchangeably. For example: selecting the maximum value of several figures

Select statement switch

format:

 

Implementation process
  • First, calculate the value of the expression
  • Secondly, compare and order case, if there is a corresponding value, will execute the corresponding statement in the process of implementation, it encounters a break will end.
  • Finally, if the case were all expressions and values ​​do not match, it will execute the default statement body part, then the program ends off

Code execution flow as shown below

Code Example

public  class Demo07Switch {
     public  static  void main (String [] args) {
         // definition of variables, of the week is determined 
        int NUM =. 4 ;
         // Switch statement to achieve select 
        Switch (NUM) {
             Case . 1 :
                System.out.println ( "Monday" );
                 BREAK ;
             Case 2 :
                System.out.println ( "Tuesday" );
                 BREAK ;
             Case 3 :
                System.out.println ( "Wednesday" );
                 BREAK ;
             Case 4 :
                System.out.println ( "Thursday" );
                 BREAK ;
             Case . 5 :
                System.out.println ( "Friday" );
                 BREAK ;
             Case 6 :
                System.out.println ( "Saturday" );
                 BREAK ;
             Case 7 :
                System.out.println ( "Sunday" );
                 BREAK ;
             default :
                System.out.println ( "Data unreasonable" );
                 break ; // the last break statement can be omitted, but it is strongly recommended not to omit 
        }
    }
}

The result of code execution

Note the use of the switch statement:

  • The latter case a plurality of values ​​can not be repeated.
  • Switch parentheses behind which only the following data types: basic data types: byte / short / char / int reference Data Type: String String, enum enum
  • The switch statement format can be very flexible: before and after the order can be reversed, and break statement can also be omitted.
  • In a switch statement, if the latter case is not writing break, will appear penetration phenomenon, which is not a value judgment in the case, run directly back, until it encounters a break, or the end of the whole switch

 

Guess you like

Origin www.cnblogs.com/wurengen/p/11906950.html