Difference between revisions of "While"

From Lazarus wiki
Jump to navigationJump to search
m (Added back page link)
Line 1: Line 1:
 
{{while}}
 
{{while}}
 +
 +
 +
Back to [[Reserved words]].
 +
  
 
<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>]].
 
<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>]].
Line 35: Line 39:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== see also ==
+
== See also ==
 +
 
 
* [[Infinite loop]]
 
* [[Infinite loop]]
  
 
{{Keywords}}
 
{{Keywords}}
[[Category:Code]]
 

Revision as of 10:55, 4 March 2020

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


Back to Reserved words.


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:

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

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).

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

See also


Keywords: begindoelseendforifrepeatthenuntilwhile