Difference between revisions of "Basic Pascal Tutorial/Chapter 3/WHILE..DO"

From Free Pascal wiki
Jump to navigationJump to search
m (Fixed syntax highlighting)
m (bypass language bar/categorization template redirect [cf. discussion])
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{WHILE..DO}}
+
{{Basic Pascal Tutorial/Chapter 3/WHILE..DO}}
{{TYNavigator|FOR..DO|REPEAT..UNTIL}}
+
{{TYNavigator|Chapter 3/FOR..DO|Chapter 3/REPEAT..UNTIL}}
  
 
== While ... DO loops ==
 
== While ... DO loops ==
Line 36: Line 36:
 
== See also ==
 
== See also ==
  
* [[FOR..DO|FOR ...DO loops]]
+
* [[Basic Pascal Tutorial/Chapter 3/FOR..DO|FOR ...DO loops]]
 
* [[Until|Repeat... Until loops]]
 
* [[Until|Repeat... Until loops]]
 
* [[for-in_loop|For... in loops]]
 
* [[for-in_loop|For... in loops]]
  
{{TYNavigator|FOR..DO|REPEAT..UNTIL}}
+
{{TYNavigator|Chapter 3/FOR..DO|Chapter 3/REPEAT..UNTIL}}

Latest revision as of 15:19, 20 August 2022

български (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

 ◄   ▲   ►