The
while Statement
üThe while statement has the following syntax:
while is
a
reserved
word
while
(condition)
statement;
If
the condition is true, the statement is executed.
Then
the condition is evaluated again.
The
statement is executed repeatedly until
the
condition becomes false.
Logic
of a while Loop
Note
that if the condition of a while statement is false initially, the statement is
never executed. Therefore, the body of a while loop will execute zero or more
times.
while
Loop Example
final
int LIMIT = 5;
int
count = 1;
while
(count <= LIMIT) {
System.out.println(count);
count += 1;
}
Infinite
Loops
üThe body of a while loop eventually
must make the condition false.
üIf not, it is an infinite
loop.
Nested
Loops
üSimilar to nested if statements,
loops can be nested as well
üThat is, the body of a loop can
contain another loop
üEach time through the outer loop,
the inner loop goes through its full set of iterations
No comments:
Post a Comment