Contents | Prev | Next
9.10 While statement
Description
While statements perform an action in a conditional loop as long as the loop
condition is true, and include the following:
- The loop condition, which is specified by an
expression of
boolean type. Before each iteration of the loop the
loop condition is evaluated and if it's value is true the
loop continues, but if it's value is false the loop
terminates.
- A statement that specifies the action performed in the loop. This statement can be
a compound statement, containing a statement-sequence.
If this statement transfers program execution to a statement outside
of the while statement then execution of the while statement is
terminated when program execution is transfered.
NOTE: The loop condition is evaluated before the statement in the
while statement is executed so this statement may never be executed. Usually the
statement in the while statement will perform some action that will eventually
cause the loop condition to be false and terminate the
execution of the loop.
Example
For example below is a very simple program which illustrates the use of the "while" statement.
program ten(output);
var
count : 1..11;
begin
count := 1;
while count <= 10 do
begin
writeln(count);
count := count + 1;
end
end.
Syntax
(NOTE: for clarity some parts of the syntax are omitted, see
Irie Pascal Grammar for the full syntax):
while-statement = 'while' boolean-expression 'do' statement
Contents | Prev | Next