Difference between revisions of "WHILE..DO"

From Lazarus wiki
m (Fixed syntax highlighting)
 
(8 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 +
{{WHILE..DO}}
 +
{{TYNavigator|FOR..DO|REPEAT..UNTIL}}
 +
 +
== While ... DO loops ==
 +
 
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:
<font color="#006699"><strong>while</strong></font> BooleanExpression <font color="#006699"><strong>do</strong></font>
+
 
  statement<font color="#000000"><strong>;</strong></font>
+
<syntaxhighlight lang=pascal>
 +
while BooleanExpression do
 +
  statement;
 +
</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:
a <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">5</font><font color="#000000"><strong>;</strong></font>
+
 
<font color="#006699"><strong>while</strong></font> a <font color="#000000"><strong>&lt;</strong></font> <font color="#ff0000">6</font> <font color="#006699"><strong>do</strong></font>
+
<syntaxhighlight lang=pascal>
  writeln <font color="#000000"><strong>(</strong></font>a<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
a := 5;
 +
while a < 6 do
 +
  writeln (a);
 +
</syntaxhighlight>
  
 
Remedy this situation by changing the variable's value:
 
Remedy this situation by changing the variable's value:
a <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">5</font><font color="#000000"><strong>;</strong></font>
+
 
<font color="#006699"><strong>while</strong></font> a <font color="#000000"><strong>&lt;</strong></font> <font color="#ff0000">6</font> <font color="#006699"><strong>do</strong></font>
+
<syntaxhighlight lang=pascal>
<font color="#006699"><strong>begin</strong></font>
+
a := 5;
  writeln <font color="#000000"><strong>(</strong></font>a<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
while a < 6 do
  a <font color="#000000"><strong>:=</strong></font> a <font color="#000000"><strong>+</strong></font> <font color="#ff0000">1</font>
+
begin
<font color="#006699"><strong>end</strong></font><font color="#000000"><strong>;</strong></font>
+
  writeln (a);
 +
  a := a + 1
 +
end;
 +
</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.
  
{|style=color-backgroud="white" cellspacing="20"
+
== See also ==
|[[FOR..DO|previous]]
+
 
|[[Contents|contents]]  
+
* [[FOR..DO|FOR ...DO loops]]
|[[REPEAT..UNTIL|next]]
+
* [[Until|Repeat... Until loops]]
|}
+
* [[for-in_loop|For... in loops]]
 +
 
 +
{{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

 ◄   ▲   ►