目录

while-do循环

Pascal中的while-do循环语句允许重复计算,直到满足某些测试条件。 换句话说,只要给定条件为真,它就会重复执行目标语句。

语法 (Syntax)

while-do循环的语法是 -

while (condition) do S;

其中, condition是布尔值或关系表达式,其值为true或false, S是BEGIN ... END块中的简单语句或语句组。

例如,

while number>0 do
begin
   sum := sum + number;
   number := number - 2;
end;

当条件变为假时,程序控制将立即传递到循环之后的行。

流程图 (Flow Diagram)

Pascal中的while-do循环

这里,while循环的关键点是循环可能永远不会运行。 当测试条件并且结果为假时,将跳过循环体并且将执行while循环之后的第一个语句。

例子 (Example)

program whileLoop;
var
   a: integer;
begin
   a := 10;
   while  a < 20  do
   begin
      writeln('value of a: ', a);
      a := a + 1;
   end;
end.

编译并执行上述代码时,会产生以下结果 -

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
↑回到顶部↑
WIKI教程 @2018