[Java] The loop, condition, and choice of java foundation

Cycle, judge, choose

One, loop

  • while loop
  • do...while loop
  • for loop

1. while () loop

public class Test {
    
    
   public static void main(String args[]) {
    
    
      int x = 10;
      while( x < 20 ) {
    
    
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }
}

2.do ……while() loop

public class Test {
    
    
   public static void main(String args[]){
    
    
      int x = 10;
 
      do{
    
    
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }while( x < 20 );
   }
}

3.for loop

public class Test {
    
    
   public static void main(String args[]) {
    
    
 
      for(int x = 10; x < 20; x = x+1) {
    
    
         System.out.print("value of x : " + x );
         System.out.print("\n");
      }
   }
}

Enhanced for loop

public class Test {
    
    
   public static void main(String args[]){
    
    
      int [] numbers = {
    
    10, 20, 30, 40, 50};
 
      for(int x : numbers ){
    
    
         System.out.print( x );
         System.out.print(",");
      }
      System.out.print("\n");
      String [] names ={
    
    "James", "Larry", "Tom", "Lacy"};
      for( String name : names ) {
    
    
         System.out.print( name );
         System.out.print(",");
      }
   }
}

break keyword

Break is mainly used in loop statements or switch statements to jump out of the entire statement block.

break Jump out of the innermost loop and continue to execute the statements below the loop.

public class Test {
    
    
   public static void main(String args[]) {
    
    
      int [] numbers = {
    
    10, 20, 30, 40, 50};
 
      for(int x : numbers ) {
    
    
         // x 等于 30 时跳出循环
         if( x == 30 ) {
    
    
            break;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}

continue keyword

continue applies to any loop control structure. The function is to make the program immediately jump to the next iteration of the loop.

In the for loop, the continue statement causes the program to immediately jump to the update statement.

In the while or do...while loop, the program immediately jumps to the judgment statement of the Boolean expression.

public class Test {
    
    
   public static void main(String args[]) {
    
    
      int [] numbers = {
    
    10, 20, 30, 40, 50};
 
      for(int x : numbers ) {
    
    
         if( x == 30 ) {
    
    
        continue;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}

2. Judgment

1. If ……else statement

public class Test {
    
    
 
   public static void main(String args[]){
    
    
      int x = 30;
 
      if( x < 20 ){
    
    
         System.out.print("这是 if 语句");
      }else{
    
    
         System.out.print("这是 else 语句");
      }
   }
}

2.if…else if…else 语句

  • The f statement has at most one else statement, and the else statement follows all else if statements.

  • The if statement can have several else if statements, and they must precede the else statement.

  • Once one of the else if statements is detected as true, the other else if and else statements will be skipped.

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 语句");
      }
   }
}

3. Nested if...else statement

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

Three, choose

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("及格");
            break;
         case 'F' :
            System.out.println("你需要再努力努力");
            break;
         default :
            System.out.println("未知等级");
      }
      System.out.println("你的等级是 " + grade);
   }
}

Switch case statement rules:

  • The variable type in the switch statement can be byte, short, int or char. Starting from Java SE 7, switch supports the String type, and the case label must be a string constant or literal.
  • The 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 a literal constant.
  • When the value of the variable is equal to the value of the case statement, the statement after the case statement starts to execute, and the switch statement will not exit until the break statement appears.
  • When a break statement is encountered, the switch statement terminates. The program jumps to the execution of the statement following the switch statement. The case statement does not have to include the break statement. If no break statement appears, the program will continue to execute the next case statement until a break statement appears.
  • The switch statement can contain a default branch, which is generally the last branch of the switch statement (it can be anywhere, but it is recommended to be the last branch). 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.

When the switch case is executed, it will be matched first, and the value of the current case will be returned if the match succeeds, and then judge whether to continue output or jump out of judgment according to whether there is a break.

If there is no break statement in the case statement block, the JVM will not output the return value corresponding to each case in sequence, but will continue to match. If the match is unsuccessful, it will return to the default case, that is, execute default.

Disclaimer: This blog post is a study note for me, referring to the rookie tutorial and other network resources, if there is any infringement, please let us know by private message!

Guess you like

Origin blog.csdn.net/qq_42380734/article/details/105348781