Difference between revisions of "WHILE..DO"

From Lazarus wiki
(See also)
Line 42: Line 42:
 
|[[REPEAT..UNTIL|next]]
 
|[[REPEAT..UNTIL|next]]
 
|}
 
|}
 
[[Category: Object Pascal Introduction]]
 

Revision as of 04:00, 25 June 2015

While ... DO loops

български (bg) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

3Db - WHILE..DO (author: Tao Yue, state: unchanged)

The pretest loop has the following format:

while BooleanExpression do
  statement;

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
begin
  writeln (a);
  a := a + 1
end;

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.

See also

FOR ...DO loops

Repeat... Until loops

For... in loops


previous contents next