处理文本文件时,可以在 While 循环中使用Continue吗?
我想做一些处理并检查一些值。如果属实,我想跳过迭代。如果为 false,我想继续下一组行(继续处理)。
while not EOF(InFile) do
begin
DoSomething;
if (AcctTag = '') OR (MasterId = '') then
Continue;
DoSomething;
end;
在这种情况下,继续会跳过迭代吗?
最佳答案
似乎 30 秒的快速测试比在这里发布帖子更快地回答这个问题。 :)
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
i, j: Integer;
begin
j := 0;
i := 0;
while i < 10 do
begin
Inc(i);
if Odd(i) then
Continue;
Inc(j);
WriteLn(Format('i = %d, j = %d', [i, j]));
end;
ReadLn;
end.
请注意,i
在调用 Continue
之前递增,这会导致 j
显示奇数,i 显示 偶数
?仅当循环通过 Continue
测试时,j
才会递增。
无论您是递增整数、连接字符串还是从文本文件中读取,while
的工作方式都是相同的。无论您如何使用它,while
都是 while
都是 while
。您只需要确保在上面的代码中,DoSomething
实际上从文件中读取下一行,否则您将陷入连续循环。
关于delphi - 在 While 循环中使用Continue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7237562/