将while循环转换为for循环

时间:2011-10-19 05:18:50

标签: delphi

我应该如何将此循环转换为for循环?

const
  TheRightWord = 'hello';
  MaximumTries = 3;
var
  NTries : integer;
  AWord : string;
begin
  NTries := 1;
  AWord := ' ';
  while (AWord <> TheRightWord) and (NTries <= MaximumTries) do

我认为这就是答案:

for (AWord <> TheRightWord) and (NTries <= MaximumTries) do

我是否只需要for代替while?或者是for i := 1 to 3 do

1 个答案:

答案 0 :(得分:7)

  for numtries := 1 to maxnumtries do begin
    if AWord = TheRightWord then
      break;
      ...

如何在退出循环时确定成功或失败(或者,就此而言,如何迭代不同的“AWord”值)是学生的练习: - )