A while statement is
similar to a repeat statement, except that the control condition is
evaluated before the first execution of the statement sequence. Hence, if the
condition is false, the statement sequence is never executed.
The syntax of a while
statement is
while expression do statement
where expression returns a Boolean
value and statement can be a compound statement. The while
statement executes its constituent statement repeatedly, testing expression
before each iteration. As long as expression returns True,
execution continues.
Examples of while
statements include
while
Data[I] <> X do I := I + 1;
while I
> 0 do
begin
if Odd(I) then Z := Z * X;
I := I div 2;
X := Sqr(X);
end;
while not
Eof(InputFile) do
begin
Readln(InputFile, Line);
Process(Line);
end;
Structured statements: Overview
while语句与repeat有些相似。不同的是,while语句控制条件的求值是在每次语句序列执行之前。因此,如果条件不满足,那么语句序列将一次都不执行。
while语句的语法是:
while expression do statement
这里的表达式expression返回一个布尔值,语句statement可以是一个混合语句。while语句重复执行其构成语句,在每次循环之前测试表达式expression。只要表达式expression返回真(True),执行就会继续下去。
下面是while语句的例子:
while
Data[I] <> X do I := I + 1;
while I
> 0 do
begin
if Odd(I) then Z := Z * X;
I := I div 2;
X := Sqr(X);
end;
while not
Eof(InputFile) do
begin
Readln(InputFile, Line);
Process(Line);
end;