The while Statement - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript The while Statement - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Monday, March 25, 2019

The while Statement

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

Post Top Ad