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

Program statements in a sequential structure can only be executed once. If you want the same operation to be performed more than once, 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 mainly used for arrays was introduced in Java5.

While loop

While is the most basic loop, its structure is:
While ( boolean expression ) { // loop content }
As long as the Boolean expression is true, the loop will continue to execute.

Examples

Test.java file 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 ( " \ the n- " ); }
}
}
The above example compiled and run results 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 while statements, if you do not satisfy the condition, you cannot enter the loop. But sometimes we need to perform at least once even if we do not meet the conditions.
The do...while loop is similar to the while loop, except that the do...while loop executes at least once.
Do { // code statement } while (boolean expression); 
       
Note: The Boolean expression is after the body of the loop, so the statement block has been executed before detecting the Boolean expression. If the value of the boolean expression is true, the statement block is executed until the value of the Boolean expression is false.

Examples

Test.java file 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 above example compiled and run results 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 be represented with while or do...while, Java provides another statement, the for loop, that makes some loop structures simpler.
The number of for loop executions is determined before execution. The syntax is as follows:
For ( initialization; Boolean expression; update ) { // code statement }
The following points 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 empty statements.
  • Then, the value of the Boolean expression is detected. If true, the loop body is executed. If false, the loop terminates and execution of the statement following the loop body begins.
  • After a loop is executed, the loop control variable is updated.
  • Test Boolean expression again. Cycle through the above process.

Examples

Test.java file 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 above example compiled and run results 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

Java5 introduced an enhanced for loop mainly for arrays.
The Java enhanced for loop syntax is formatted as follows:
For ( declaration statement: expression ) { // code sentence }
Declaration statement: declare a new local variable whose type must match the type of the array element. 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 of values.

Examples

Test.java file 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 above example compiled and run results 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 entire statement block.
Break jumps out of the innermost loop and continues executing the statement below the loop.

grammar

The use of break is very simple, it is a statement in the loop structure:
BREAK ;

Examples

Test.java file 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 above example compiled and run results are as follows:
10 20

Continue keyword

Continue applies to any loop control structure. The effect is to let 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 a while or do...while loop, the program immediately jumps to the boolean expression's statement.

grammar

Continue is a simple statement in the loop body:
Continue ;

Examples

Test.java file 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 above example compiled and run results are as follows:
10 20 40 50

Comments