next up previous
Next: Use of a while Up: Unit 06 Previous: Definite and indefinite loops

The while loop

The while statement allows for the la repetition of a statement.


while statement

Syntax:

while (condition)
  statement

Note: since, by making use of a block, it is possible to group several statements into a single composite statement, it is in fact possible to have more than one statement in the body of the loop.

Semantics:

Hence, the body of the loop is executed as long as the condition stays true. As soon as it becomes false we exit the loop and continue with the following statement.

Example: Print out 100 stars.

int i = 0;
while (i < 100) {
  System.out.print("*");
  i++;
}


next up previous
Next: Use of a while Up: Unit 06 Previous: Definite and indefinite loops