Java Loop Structures - for, while and do...while
A program statement in sequential structure can only be executed once.
If you want the same operation to be executed multiple times, you need to use a loop structure.
There are three main loop structures in Java:
- whileLoop
- do…whileLoop
- forLoop
In Java 5, an enhanced for loop mainly used for arrays was introduced.
while loop
while is the most basic loop, and its structure is:
As long as the Boolean expression is true, the loop will keep executing.
Example
Test.java file code:
The result of compiling and running the above example is 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, the loop cannot be entered. But sometimes we need to execute at least once even if the condition is not met.
The do...while loop is similar to the while loop, except that the do...while loop is executed at least once.
do {
//代码语句
}while(布尔表达式);
Note:The Boolean expression is after the loop body, so the statement block is executed before the Boolean expression is tested. If the value of the Boolean expression is true, the statement block keeps executing until the value of the Boolean expression is false.
Example
Test.java file code:
The result of compiling and running the above example is 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, which makes some loop structures simpler.
The number of executions of a for loop is determined before execution. The syntax format is as follows:
The following points explain the for loop:
- The initialization step is executed first. A type can be declared, one or more loop control variables can be initialized, or it can be an empty statement.
- Then, the value of the Boolean expression is tested. If it is true, the loop body is executed. If it is false, the loop terminates and execution begins on the statements after the loop body.
- After the loop is executed once, the loop control variables are updated.
- Test the Boolean expression again. The loop repeats the above process.
Example
Test.java file code:
The result of compiling and running the above example is 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 introduced an enhanced for loop mainly used for arrays.
The syntax format of the Java enhanced for loop is as follows:
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 current array element.
Expression:The expression is the name of the array to be accessed, or a method whose return value is an array.
Example
Test.java file code:
The result of compiling and running the above example 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 break out of the entire statement block.
break breaks out of the innermost loop and continues executing the statements below the loop.
Syntax
The usage of break is very simple; it is just a statement in the loop structure:
break;
Example
Test.java file code:
The result of compiling and running the above example is as follows:
10 20
continue keyword
continue applies to any loop control structure. Its effect is to make the program 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 test statement.
Syntax
continue is simply a single statement in the loop body:
continue;
Example
Test.java file code:
The result of compiling and running the above example is as follows:
10 20 40 50Other Extensions