Java beginner's loop structure

There are many loop structures in java, including for loop, enhanced for loop, while loop, and enhanced for loop.

while loop, its structure is:

while(boolean expression) {
       //loop content
}

 

 As long as the boolean expression is not true, the loop will continue to execute.

do-while loop, its structure is:

do{
  //code statement
}while(boolean expression);

For the while statement, if the condition is not met, the loop will not be entered. But sometimes we need to execute at least once even if the condition is not met. The do-while loop is very similar to the while loop except that the do-while is executed at least once.

 

for loop: The number of times the for loop executes is determined at the beginning.

The syntax is

for(initialization; boolean expression; iteration) {
      code block
}

 A few notes about the for loop:

  • The initialization step is performed first. A type can be declared, but one or more loop control variables can be initialized, or it can be an empty statement.
  • Then, check the value of the Boolean expression. If true, the loop body is executed. If false, the loop terminates and execution of the statements following the loop body begins.
  • After executing the loop once, update the loop control variable.
  • Check the boolean expression again. Loop through the above process.

java enhanced for loop

java5 introduced an enhanced for loop mainly for arrays.

java enhanced for loop syntax structure:

for(declaration statement: expression) {
          //code block
}

 

Declaration Statement: Declare a new local variable whose type must match the type of the array elements. Its scope is limited to the loop statement block, and its value is equal to the value of the array element at this time.

Expression: The expression is the name of the array to access, or a method that returns an array.

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

 After the above example is compiled and run, the result is as follows:

10,20,30,40,50,
James,Larry,Tom,Lacy,

 break keyword:

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

break out of the innermost loop, and continue to execute the statement below the loop.

example code

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

After the example is compiled and run:

10
20

 continue keyword:,

ontinue works in any loop control structure. The effect is to make the program jump to the next iteration of the loop immediately.

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

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

Example:

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

 After running:

10
20
40
50

 

  

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326861294&siteId=291194637