# while

The keyword is used...

1. alone to define a loop where the loop condition is checked before of each new loop iteration (see below);
2. together with the keyword do to define a loop where the loop condition is checked before of each new loop iteration.

The keyword defines on its own a loop which is executed as long as the loop condition holds. The loop condition shall be checked at the beginning of each new loop iteration. If the loop condition does not hold, then the loop is exited and execution shall continue with the statement, which immediately follows the loop.

### while ( condition) { statement_block };

- The while keyword introduces the loop.

- condition is a Boolean expression. It is checked at the beginning of each new loop iteration. If it evaluates to false, the loop terminates.

- statement_block contains one or more statements. The block will be executed in a loop provided condition is true.

Example 1:

var integer i := 3;
while (i<=10) {i:= i+2 }

The loop variable i is declared in the first line and it is given the initial value 3. In the second line, a loop is defined. The loop execution terminates when the index value will have a value greater than 10. In the statement block the index variable is increased by 2 every time the loop is executed. The loop will be executed 5 times.

BNF definition of while