Difference between revisions of "WHILE..DO"

From Lazarus wiki
(See also)
m (Fixed syntax highlighting)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
 +
{{WHILE..DO}}
 +
{{TYNavigator|FOR..DO|REPEAT..UNTIL}}
 +
 
== While ... DO loops ==
 
== While ... DO loops ==
{{WHILE..DO}}
 
  
 
3Db - WHILE..DO (author: Tao Yue, state: unchanged)
 
3Db - WHILE..DO (author: Tao Yue, state: unchanged)
  
 
The pretest loop has the following format:
 
The pretest loop has the following format:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
while BooleanExpression do
 
while BooleanExpression do
 
   statement;
 
   statement;
Line 11: Line 14:
  
 
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:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
a := 5;
 
a := 5;
 
while a < 6 do
 
while a < 6 do
Line 18: Line 22:
  
 
Remedy this situation by changing the variable's value:
 
Remedy this situation by changing the variable's value:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
a := 5;
 
a := 5;
 
while a < 6 do
 
while a < 6 do
Line 30: Line 35:
  
 
== See also ==
 
== See also ==
[[FOR..DO|FOR ...DO loops]]
 
 
[[Until|Repeat... Until loops]]
 
 
[[for-in_loop|For... in loops]]
 
 
  
{|style=color-backgroud="white" cellspacing="20"
+
* [[FOR..DO|FOR ...DO loops]]
|[[FOR..DO|previous]]
+
* [[Until|Repeat... Until loops]]
|[[Contents|contents]]  
+
* [[for-in_loop|For... in loops]]
|[[REPEAT..UNTIL|next]]
 
|}
 
  
[[Category: Object Pascal Introduction]]
+
{{TYNavigator|FOR..DO|REPEAT..UNTIL}}

Latest revision as of 10:50, 4 March 2020

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

 ◄   ▲   ► 

While ... DO loops

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

 ◄   ▲   ►