Difference between revisions of "While"

From Lazarus wiki
Jump to navigationJump to search
m
(more content)
Line 1: Line 1:
 
{{while}}
 
{{while}}
<br>
+
 
'''While''' repeats a block of statements while a condition is true.
+
<syntaxhighlight lang="pascal" enclose="none">while</syntaxhighlight> in conjunction with [[Do|<syntaxhighlight lang="pascal" enclose="none">do</syntaxhighlight>]] repeats a statement as long as a condition evaluates to [[True|<syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>]].
  while (condition) do  
+
The condition expression is evaluated prior each iteration, determining whether the following block (or single statement) is executed.
    (block of statements);
+
This is the main difference to a [[Repeat|<syntaxhighlight lang="pascal" enclose="none">repeat … until</syntaxhighlight>-loop]], where the block is executed at any rate, but succeeding iterations do not necessarily happen, though.
 +
 
 +
The following example contains unreachable code:
 +
<syntaxhighlight lang="pascal" line start="0">
 +
program whileFalse(input, output, stderr);
 +
 
 +
begin
 +
while false do
 +
begin
 +
writeLn('never gets printed');
 +
end;
 +
end.
 +
</syntaxhighlight>
 +
 
 +
You usually use <syntaxhighlight lang="pascal" enclose="none">while</syntaxhighlight>-loops where, in contrast to [[For|<syntaxhighlight lang="pascal" enclose="none">for</syntaxhighlight>-loops]], a running index variable is not required, the block executed can't be deduced from an index that's incremented by one, or to avoid a [[Break|<syntaxhighlight lang="pascal" enclose="none">break</syntaxhighlight>-statement]] (which usually indicates bad programming style).
 +
<syntaxhighlight lang="pascal" line start="0">
 +
program whileDemo(input, output, stderr);
 +
 
 +
var
 +
x: integer;
 +
begin
 +
x := 1;
 +
 +
// prints non-negative integer powers of two
 +
while x < high(x) div 2 do
 +
begin
 +
writeLn(x);
 +
inc(x, x);
 +
end;
 +
end.
 +
</syntaxhighlight>
 +
 
 +
== see also ==
 +
* [[Infinite loop]]
  
 
{{Keywords}}
 
{{Keywords}}
 +
[[Category:Code]]

Revision as of 00:11, 14 February 2018

Deutsch (de) English (en) suomi (fi) français (fr) русский (ru)

while in conjunction with do repeats a statement as long as a condition evaluates to true. The condition expression is evaluated prior each iteration, determining whether the following block (or single statement) is executed. This is the main difference to a repeat  until-loop, where the block is executed at any rate, but succeeding iterations do not necessarily happen, though.

The following example contains unreachable code:

0program whileFalse(input, output, stderr);
1
2begin
3	while false do
4	begin
5		writeLn('never gets printed');
6	end;
7end.

You usually use while-loops where, in contrast to for-loops, a running index variable is not required, the block executed can't be deduced from an index that's incremented by one, or to avoid a break-statement (which usually indicates bad programming style).

 0program whileDemo(input, output, stderr);
 1
 2var
 3	x: integer;
 4begin
 5	x := 1;
 6	
 7	// prints non-negative integer powers of two
 8	while x < high(x) div 2 do
 9	begin
10		writeLn(x);
11		inc(x, x);
12	end;
13end.

see also


Keywords: begindoelseendforifrepeatthenuntilwhile