目录

repeat-until循环

与在循环顶部测试循环条件的for和while循环不同,Pascal中的repeat ... until循环检查循环底部的条件。

重复...直到循环类似于while循环,除了重复... until循环保证至少执行一次。

语法 (Syntax)

repeat
   S1;
   S2;
   ...
   ...
   Sn;
until condition;

例如,

repeat
   sum := sum + number;
   number := number - 2;
until number = 0;

请注意,条件表达式出现在循环的末尾,因此循环中的语句在测试条件之前执行一次。

如果条件为假,则控制流跳回到重复,并且循环中的语句再次执行。 重复此过程直到给定条件变为真。

流程图 (Flow Diagram)

重复直到Pascal循环

例子 (Example)

program repeatUntilLoop;
var
   a: integer;
begin
   a := 10;
   (* repeat until loop execution *)
   repeat
      writeln('value of a: ', a);
      a := a + 1
   until a = 20;
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