Basic Pascal Tutorial/Chapter 3/WHILE..DO

From Free Pascal wiki
Revision as of 11:57, 21 November 2007 by Kees (talk | contribs) (New page: 3Db - WHILE..DO The pretest loop has the following format: <font color="#006699"><strong>while</strong></font> BooleanExpression <font color="#006699"><strong>do</strong></font> state...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search


The pretest loop has the following format:

while BooleanExpression do

The loop continues to execute until the Boolean expression becomes FALSE. In the body of the loop, you must somehow affect the Boolean expression by changing one of the variables used in it. Otherwise, an infinite loop will result:

a := 5;
while a < 6 do
  writeln (a);

Remedy this situation by changing the variable's value:

a := 5;
while a < 6 do
  writeln (a);
  a := a + 1

The WHILE ... DO loop is called a pretest loop because the condition is tested before the body of the loop executes. So if the condition starts out as FALSE, the body of the while loop never executes.

previous contents next