Java Loop Structures - for, while, and do...while
In a sequence structure, program statements 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:
- while Loop
- do...while Loop
- for Loop
An enhanced for loop, mainly used for arrays, was introduced in Java 5.
while loop
while is the most basic loop; its structure is:
while
(
boolean expression
)
{
//
Loop body
}
As long as the boolean expression is true, the loop continues to execute.
Example
Test.java File Code:
public
class
Test
{
public
static
void
main
(
String
[
]
args
)
{
int
x
=
10
;
while
(
x
<
20
)
{
System
.
out
.
(
"
value of x :
"
x
)
;
x
++;
System
.
out
.
(
"
\n
"
)
;
}
}
}
The compilation and execution results of the above example 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 a while statement, if the condition is not met, the loop cannot be entered. But sometimes we need to execute at least once even when the condition is not met.
The do...while loop is similar to the while loop, except that the do...while loop executes at least once.
do {
//code statement
}while(布尔表达式);
**Note:** The boolean expression is after the loop body, so the statement block is executed before the boolean expression is evaluated. If the value of the boolean expression is true, the statement block keeps executing until the value of the boolean expression becomes false.
Example
Test.java File Code:
public
class
Test
{
public
static
void
main
(
String
[
]
args
)
{
int
x
=
10
;
do
{
System
.
out
.
(
"
value of x :
"
x
)
;
x
++;
System
.
out
.
(
"
\n
"
)
;
}
while
(
x
<
20
)
;
}
}
The compilation and execution results of the above example 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 expressed using 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
}
The following points explain the for loop:
- 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 tested. If it is true, the loop body is executed. If it is false, the loop terminates and execution continues with the statement after the loop body.
- After one loop execution, the loop control variable is updated.
- The boolean expression is tested again. The loop repeats the above process.
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
.
(
"
value of x :
"
x
)
;
System
.
out
.
(
"
\n
"
)
;
}
}
}
The compilation and execution results of the above example 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 introduced an enhanced for loop mainly used for arrays.
The syntax of the Java enhanced for loop is as follows:
for
(
Declaration statement : Expression
)
{
//
Code statement
}
**Declaration statement:** Declares a new local variable whose type must match the type of the array element. Its scope is limited to the loop block, and its value equals the value of the array element at that moment.
**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
.
(
x
)
;
System
.
out
.
(
"
,
"
)
;
}
System
.
out
.
(
"
\n
"
)
;
String
[
]
names
=
{
"
James
"
,
"
Larry
"
,
"
Tom
"
,
"
Lacy
"
}
;
for
(
String
name
:
names
)
{
System
.
out
.
(
name
)
;
System
.
out
.
(
"
,
"
)
;
}
}
}
The compilation and execution results of the above example 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 break out of the entire statement block.
break jumps out of the innermost loop and continues executing the statement below that loop.
Syntax
The usage of break is simple; it is just a statement in 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 out of the loop when x equals 30
if
(
x
==
30
)
{
break
;
}
System
.
out
.
(
x
)
;
System
.
out
.
(
"
\n
"
)
;
}
}
}
The compilation and execution results of the above example are as follows:
10
20
continue keyword
continue applies to any loop control structure. Its effect is to make the program jump immediately to the next iteration of the loop.
In a for loop, the continue statement causes the program to jump immediately to the update statement.
In a while or do...while loop, the program jumps immediately to the evaluation of the boolean expression.
Syntax
continue is simply a statement in 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
.
(
x
)
;
System
.
out
.
(
"
\n
"
)
;
}
}
}
The compilation and execution results of the above example are as follows:
10
20
40
50