Java Loop Structures - for, while, and do...while
Sequential program statements can only be executed once.
If you want the same operation to be executed multiple times, you need to use loop structures.
Java has three main loop structures:
- while loop
- do…while loop
- for loop
An enhanced for loop, primarily for arrays, was introduced in Java 5.
while Loop
The while loop is the most basic loop, and its structure is:
while( boolean expression ) {
// loop content
}
As long as the boolean expression is true, the loop will continue to execute.
Example
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("\n");
}
}
}
The above example compiles and runs with the following result:
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 a while statement, if the condition is not met, the loop cannot be entered. However, sometimes we need to execute the loop at least once even if the condition is not met.
The do…while loop is similar to the while loop, but the do…while loop will execute at least once.
do {
// code statement
} while( boolean expression );
Note: The boolean expression is after the loop body, so the block of statements has already been executed before the boolean expression is checked. If the boolean expression's value is true, the block of statements will keep executing until the boolean expression's value becomes false.
Example
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 compiles and runs with the following result:
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 — which makes some loop structures simpler.
The number of times a for loop executes is determined before execution. The syntax is as follows:
for(initialization; boolean expression; update) {
// code statement
}
Regarding the for loop, the following points should be noted:
- The initialization step is executed first. You can declare a type, but you can initialize one or more loop control variables, or it can be an empty statement.
- Then, the value of the boolean expression is checked. If it is true, the loop body is executed. If it is false, the loop terminates and starts executing the statement after the loop body.
- After executing the loop once, the loop control variable is updated.
- The boolean expression is checked again. The process above is repeated.
Example
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 compiles and runs with the following result:
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
Enhanced For Loop in Java
Java 5 introduced an enhanced for loop primarily used for arrays.
The syntax for the enhanced for loop in Java is as follows:
for(declaration : expression)
{
// Code statements
}
Declaration: Declares a new local variable, whose type must match the type of the array elements. Its scope is limited to the loop block, and its value is equal to the current array element.
Expression: The expression is the name of the array to be accessed, or a method that returns an array.
Example
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 compilation and execution result of the above example is as follows:
10,20,30,40,50,
James,Larry,Tom,Lacy,
break Keyword
The break keyword is mainly used in loop or switch statements to exit the entire block.
The break exits the innermost loop and continues executing the statements below the loop.
Syntax
The usage of break is straightforward, it is a statement within a loop structure:
break;
Example
Test.java File Code:
public class Test {
public static void main(String[] args) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
// Break the loop when x equals 30
if( x == 30 ) {
break;
}
System.out.print( x );
System.out.print("\n");
}
}
}
The compilation and execution result of the above example is as follows:
10
20
continue Keyword
The continue keyword is applicable in any loop control structure. It causes the program to immediately jump to the next iteration of the loop.
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 boolean expression evaluation statement.
Syntax
The continue statement is a simple statement within the loop body:
continue;
Example
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 compilation and execution result of the above example is as follows:
10
20
40
50