Difference between revisions of "While"
(more content) |
(undo revision 025DCC by Paskal (talk): this content is rather presented on Continue and, by the way, the example is imaginably poorly designed) Tag: Undo |
||
(11 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
{{while}} | {{while}} | ||
− | <syntaxhighlight lang="pascal" | + | <syntaxhighlight lang="pascal" inline>while</syntaxhighlight> in conjunction with [[Do|<syntaxhighlight lang="pascal" inline>do</syntaxhighlight>]] repeats a statement as long as a condition evaluates to [[True|<syntaxhighlight lang="pascal" inline>true</syntaxhighlight>]]. |
− | The condition expression is evaluated prior each iteration, determining whether the following | + | The condition [[expression]] is evaluated prior each iteration, determining whether the following [[statement]] is executed. |
− | This is the main difference to a [[Repeat|<syntaxhighlight lang="pascal" | + | This is the main difference to a [[Repeat|<syntaxhighlight lang="pascal" inline>repeat … until</syntaxhighlight>-loop]], where the [[Loops|loop]] body is executed at any rate, but succeeding iterations do not necessarily happen, though. |
The following example contains unreachable code: | The following example contains unreachable code: | ||
− | <syntaxhighlight lang="pascal" line | + | <syntaxhighlight lang="pascal" line highlight="4"> |
program whileFalse(input, output, stderr); | program whileFalse(input, output, stderr); | ||
Line 17: | Line 17: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | You usually use <syntaxhighlight lang="pascal" | + | You usually use <syntaxhighlight lang="pascal" inline>while</syntaxhighlight>-loops where, in contrast to [[For|<syntaxhighlight lang="pascal" inline>for</syntaxhighlight>-loops]], a running index [[Variable|variable]] is not required, the statement executed can't be deduced from an index that's incremented by one, or to avoid a [[Break|<syntaxhighlight lang="pascal" inline>break</syntaxhighlight>-statement]] (which usually indicates bad programming style). |
− | <syntaxhighlight lang="pascal" line | + | <syntaxhighlight lang="pascal" line highlight="9"> |
program whileDemo(input, output, stderr); | program whileDemo(input, output, stderr); | ||
Line 30: | Line 30: | ||
begin | begin | ||
writeLn(x); | writeLn(x); | ||
− | inc(x, x); | + | inc(x, x); // x := x + x |
end; | end; | ||
end. | end. |
Latest revision as of 06:22, 25 January 2023
│
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 statement is executed.
This is the main difference to a repeat … until
-loop, where the loop body 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 statement 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: begin — do — else — end — for — if — repeat — then — until — while