Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java loop structure - for, while and do... while


May 10, 2021 Java


Table of contents


Java loop structure - for, while and do... while

Program statements for sequential structures can only be executed once. I f you want to do the same thing multiple times, you need to use a circular structure.

There are three main loop structures in Java:

  • While loop
  • do... While loop
  • For loop

In Java 5, an enhanced for loop is introduced that is mainly used for arrays.


While loop

While is the most basic cycle, and its structure is:

while( 布尔表达式 ) {
	//循环内容
}

As long as the Boolean expression is true, the loop experience continues.

Instance

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

The above examples compile and run as follows:

value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

do... While loop

For while statements, you cannot enter a loop if the condition is not met. But sometimes we need to do it at least once, even if we don't meet the criteria.

do... W hile loops are similar to while loops, the difference is that do... The while loop is executed at least once.

do {
       //代码语句
}while(布尔表达式);

Note: The Boolean expression is behind the loop body, so the statement block was executed before the Boolean expression was detected. I f the value of the Boolean expression is true, the statement block is executed until the value of the Boolean expression is true.

Instance

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

The above examples compile and run as follows:

value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

For loop

Although all loop structures can be used while or do... W hile represents, but Java provides another statement -- the for loop, which makes some loop structures simpler.

The number of times the for loop is executed is determined before execution. The syntax format is as follows:

for(初始化; 布尔表达式; 更新) {
    //代码语句
}

Here are a few instructions about the for loop:

  • The initialization step is performed first. You can declare a type, but you can initialize one or more loop control variables, or you can be an empty statement.
  • Then, detect the value of the Boolean expression. I f true, the loop body is executed. If it is false, the loop terminates and the statement after the loop body is executed.
  • After a loop is executed, update the loop control variable.
  • The Boolean expression is detected again. Loop through the above procedure.

Instance

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

The above examples compile and run as follows:

value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

Java enhances the for loop

Java5 introduces an enhanced for loop that is primarily used for arrays.

The Java Enhanced for loop syntax format is as follows:

for(声明语句 : 表达式)
{
   //代码句子
}

Declaration statement: Declares a new local variable, the type of which must match the type of the array element. I ts scope is limited to the loop statement block, whose value is equal to the value of the array element at this time.

Expression: The name of the array that the expression is accessing, or the method that returns a value of the array.

Instance

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

The above examples compile and run as follows:

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

Break keyword

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

Break jumps out of the middleth loop and continues with the statement below the loop.

Grammar

Break is simply used as a statement in the loop structure:

break;

Instance

public class Test {

   public static void main(String args[]) {
      int [] numbers = {10, 20, 30, 40, 50};

      for(int x : numbers ) {
         if( x == 30 ) {
	      break;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}

The above examples compile and run as follows:

10
20

The continue keyword

Continue is suitable for any loop control structure. T he effect is to get the program to jump immediately to the iteration of the next loop.

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

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

Grammar

Continue is a simple statement in a loop body:

continue;

Instance

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

The above examples compile and run as follows:

10
20
40
50