Next: , Previous: , Up: Expressions And Statements   [Index]


7.2 The While And Do While Statements

The two statements provide for cyclic code execution. The ‘while’ statement executes a block for as long as an initial condition passes. The ‘do’ ‘while’ executes the block until a final condition fails.

The syntax of the ‘while’ statement is similar to that of the simple ‘if’ statement, with the ‘while’ keyword replacing the ‘if’.

    integer a;

    a = 0;
    while (a < 16) {
	o_space(1);
	o_integer(a);
	a += 1;
    }

The fragment prints:

 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

The condition is enclosed by parantheses and following the ‘while’ keyword.

The syntax of the ‘do’ ‘while’ statement has the condition placed after the block to execute and a final semicolon completing it.

    integer a;

    a = 32;
    do {
	a -= 1;
	o_space(1);
	o_integer(a);
    } while (16 < a);

The fragment prints:

 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16