Java loop structure - for, while and do while

Posted by abhi201090 on Mon, 07 Feb 2022 04:24:16 +0100

Sequential program statements can only be executed once.

If you want to perform the same operation multiple times, you need to use a loop structure.

There are three main loop structures in Java:

while loop

do... while loop

for loop

An enhanced for loop is introduced in Java 5, which is mainly used for arrays.

while loop

while is the most basic loop. Its structure is:

While (Boolean expression){
//Cyclic content
}

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

example

Test.java Document code:
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 compilation and operation results of the above examples are 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 the while statement, if the condition is not met, it cannot enter the loop. Sometimes we don't need to meet the conditions at least once.

The do... While loop is similar to the while loop, except that the do... While loop is executed at least once.

do {

   //Code statement

}While (Boolean expression);

Note: the Boolean expression follows the loop body, so the statement block has been executed before detecting the Boolean expression. If the value of the Boolean expression is true, the statement block executes until the value of the Boolean expression is false.

example

Test.java Document code:
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 compilation and operation results of the above examples are 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 use while or do While, but Java provides another statement - 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 (initialization; Boolean expression; update){

//Code statement

}

There are the following explanations for the for loop:

Perform the initialization step first. You can declare a type, but you can initialize one or more loop control variables or empty statements.

Then, the value of the Boolean expression is detected. If true, the loop body is executed. If false, the loop terminates and starts executing the statement after the loop body.

After executing a loop, update the loop control variable.

Detect the Boolean expression again. Loop through the above procedure.

example

Test.java Document code:
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 compilation and operation results of the above examples are 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 enhanced for loop

Java 5 introduces an enhanced for loop that is mainly used for arrays.

The syntax format of Java enhanced for loop is as follows:

For (declaration statement: expression)
{
//Code sentence
}

Declaration statement: declare a new local variable. The type of the variable must match the type of the array element. Its scope is limited to the circular statement block, and its value is equal to the value of the array element at this time.

Expression: an expression is the name of the array to be accessed, or a method whose return value is an array.

example

Test.java Document code:
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 compilation and operation results of the above examples are 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 whole statement block.

break jumps out of the innermost loop and continues to execute the following statements of the loop.

grammar

The usage of break is very simple. It is a statement in the loop structure:

break;

example

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

The compilation and operation results of the above examples are as follows:

10
20

continue keyword

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

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 Boolean expression.

grammar

continue is a simple statement in the loop body:

continue;

example

Test.java Document code:
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 compilation and operation results of the above examples are as follows:

10
20
40
50

key word: java training

Topics: Java