-if-switch branch structure java-

The if statement

Format 1

  format:

    if (relational expression) {

      Statement body;

    }

public  class IfDemo { 
  public  static  void main (String [] args) { 
    System.out.println ( "Start"); // define two variables 
      int a = 10 ; 
      int b = 20 is; // needs: Analyzing a and b the values are equal, if they are equal, the output on the console: a is equal to B 
      IF (a == B) { 
         System.out.println ( "a equals B" ); 
      } // requirements: determining whether the values of a and c are equal if they are equal, the output on the console: a is equal to C 
      int C = 10 ; 
       IF (A == C) { 
          System.out.println ( "A is equal to C" ); 
      } 
      System.out.println ("End" ); 
  } }

Format 2

  format:
    if (relational expression) {
      Statement 1;
    } else {
      Statement 2;
    }
public  class IfDemo02 {
      public  static  void main (String [] args) { 
         System.out.println ( "Start"); // define two variables 
                int A = 10 ;
                 int B = 20 is ; 
                B =. 5; // needs: determining whether a than b, that if, in the console output: the value of a than b, that otherwise, in the console output: the value of a is not more than B 
                 IF (a> B) { 
                        System.out.println ( "a value of greater than B " ); 
                 } the else { 
                        System.out.println ( " a value not greater than B " ); 
                 }
                 System.out.println ( "End"); 
}
}

   Parity the number of cases

Import java.util.Scanner; 
 public  class IfTest01 { 
      public  static  void main (String [] args) { 
        Scanner SC = new new Scanner (the System.in); 
        System.out.println ( "Please enter an integer:" );
          int Number = sc.nextInt (); 
            // Analyzing is even or odd integer to judge two cases, the structure using if..else
           // determines whether the even requires the operator to take over the functions implemented% 2 == 0 Number 
           / / according to the determination, the corresponding content in the console output 
        IF (Number% 2 == 0 ) { 
                System.out.println (Number + "is an even number" ); 
         }the else { 
                System.out.println (Number + "is an odd number" ); 
         } 
      } 
}

Format 3

    

  format:
  if (relational expression 1) {
    Statement 1;
  } Else if (relational expression 2) {
    Statement 2;
  }
  …
  else {
    Body statement n + 1;
  }
 
Implementation process:
① First, the relationship between the calculated value of the expression 1
② If true statement 1 is executed; if the value false to the value calculated relational expression 2
2 ③ If true body statement is executed; if the value false to the value calculated relational expression 3
④…
⑤ If there is no relationship between the expression is true, then the statement is executed body n + 1.

switch statement

 format

  switch (expression) {
    case 1 target:
      Statement 1 is executed;
      break;
    case 2 target values:
      Execute the statement 2;
      break;
      ....
    case the target value of n:
      Execute the statement n;
      break;
    default:
      Executing the statement n + 1;
      break;

Implementation process

1. First find the result of the expression.
2. Use the results and the subsequent expression of the case to match the target value, which can match the target value, then the execution of the target language which later is executed
Sentence, then encountered break, jump swtich. If all of the target values ​​are not matched, the code will execute default.
 

switch to penetrate

In a switch statement may be omitted break, if you omit the break, a complete statement of code execution back case, will continue to penetrate down to
Under execution in a case of executing the statement, until it encounters the break, also known as the penetrating swtich

Guess you like

Origin www.cnblogs.com/ljq697/p/12608856.html