Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Loop Structures – for, while, and do...while

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:

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

.

print

(

"

value of x :

"

x

)

;

x

++;

System

.

out

.

print

(

"

\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

.

print

(

"

value of x :

"

x

)

;

x

++;

System

.

out

.

print

(

"

\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:

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 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

.

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 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

.

print

(

x

)

;

System

.

out

.

print

(

"

\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

.

print

(

x

)

;

System

.

out

.

print

(

"

\n

"

)

;

}

}

}

The compilation and execution results of the above example are as follows:


10
20
40
50