Difference between revisions of "WHILE..DO"

From Lazarus wiki
m (Text replace - "delphi>" to "syntaxhighlight>")
Line 2: Line 2:
  
 
The pretest loop has the following format:
 
The pretest loop has the following format:
<delphi>
+
<syntaxhighlight>
 
while BooleanExpression do
 
while BooleanExpression do
 
   statement;
 
   statement;
</delphi>
+
</syntaxhighlight>
  
 
The loop continues to execute until the Boolean expression becomes <tt>FALSE</tt>. 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:
 
The loop continues to execute until the Boolean expression becomes <tt>FALSE</tt>. 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:
<delphi>
+
<syntaxhighlight>
 
a := 5;
 
a := 5;
 
while a < 6 do
 
while a < 6 do
 
   writeln (a);
 
   writeln (a);
</delphi>
+
</syntaxhighlight>
  
 
Remedy this situation by changing the variable's value:
 
Remedy this situation by changing the variable's value:
<delphi>
+
<syntaxhighlight>
 
a := 5;
 
a := 5;
 
while a < 6 do
 
while a < 6 do
Line 22: Line 22:
 
   a := a + 1
 
   a := a + 1
 
end;
 
end;
</delphi>
+
</syntaxhighlight>
  
 
The <tt>WHILE ... DO</tt> 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 <tt>FALSE</tt>, the body of the <tt>while</tt> loop never executes.
 
The <tt>WHILE ... DO</tt> 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 <tt>FALSE</tt>, the body of the <tt>while</tt> loop never executes.

Revision as of 16:07, 24 March 2012

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.

previous contents next