Difference between revisions of "While"
(Undo revision 133947 by Trev (talk): there are multiple pages referring to this page) Tag: Undo |
(Clean up text a little) |
||
Line 1: | Line 1: | ||
{{while}} | {{while}} | ||
− | + | The [[Reserved word|reserved word]] {{HL|while}} is used as a [[loop instruction]] where the test occurs before the loop is executed. It is a control construct that is similar to a [[Repeat|{{HL|repeat}}]]-[[Until|{{HL|until}}]] loop. Unlike {{HL|for}} or {{HL|repeat}} loops, {{HL|while}} is the only type of loop where the loop might not be executed at all. | |
− | The condition [[expression]] is evaluated prior each iteration, determining whether the following block (or single statement) is executed. | + | |
− | This is the main difference | + | While shares a characteristic with {{HL|for}} loops, in that the {{HL|while ... do}} construct only covers a single statement. If more than one statement needs to be executed in the loop, a {{HL|begin ... end}} block is required. |
+ | |||
+ | The {{HL|while}} statement, in conjunction with [[Do|{{HL|do}}]] repeats a statement as long as a condition evaluates to [[True|{{HL|true}}]]. | ||
+ | The condition [[expression]] is evaluated prior to each iteration, determining whether the following block (or single statement) is to be executed. If the condition is false to begin with, the loop is never executed. This is the main difference from a {{HL|repeat … until}}-loop, where the [[Block|block]] is executed at least once, but succeeding iterations do not necessarily happen, though. | ||
The following example contains unreachable code: | The following example contains unreachable code: | ||
Line 17: | Line 20: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | You usually use | + | You usually use {{HL|while}}-loops where, in contrast to [[For|{{HL|for}}]], a running index [[Variable|variable]] is not required, the block executed can't be deduced from an index that's incremented by one, or to avoid a [[Break|{{HL|break}} statement]] (which usually indicates bad programming style). |
<syntaxhighlight lang="pascal" line highlight="9"> | <syntaxhighlight lang="pascal" line highlight="9"> | ||
program whileDemo(input, output, stderr); | program whileDemo(input, output, stderr); | ||
Line 40: | Line 43: | ||
{{Keywords}} | {{Keywords}} | ||
[[Category:Code]] | [[Category:Code]] | ||
+ | [[Category:Pascal]] |
Revision as of 00:41, 15 October 2020
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
русский (ru) │
The reserved word while
is used as a loop instruction where the test occurs before the loop is executed. It is a control construct that is similar to a repeat
-until
loop. Unlike for
or repeat
loops, while
is the only type of loop where the loop might not be executed at all.
While shares a characteristic with for
loops, in that the while ... do
construct only covers a single statement. If more than one statement needs to be executed in the loop, a begin ... end
block is required.
The while
statement, in conjunction with do
repeats a statement as long as a condition evaluates to true
.
The condition expression is evaluated prior to each iteration, determining whether the following block (or single statement) is to be executed. If the condition is false to begin with, the loop is never executed. This is the main difference from a repeat … until
-loop, where the block is executed at least once, 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
, 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: begin — do — else — end — for — if — repeat — then — until — while