delphi - 在 While 循环中使用Continue

标签 delphi delphi-7

处理文本文件时,可以在 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.

Sample output

请注意,i 在调用 Continue 之前递增,这会导致 j 显示奇数​​,i 显示 偶数?仅当循环通过 Continue 测试时,j 才会递增。

无论您是递增整数、连接字符串还是从文本文件中读取,while 的工作方式都是相同的。无论您如何使用它,while 都是 while 都是 while。您只需要确保在上面的代码中,DoSomething 实际上从文件中读取下一行,否则您将陷入连续循环。

关于delphi - 在 While 循环中使用Continue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7237562/

相关文章:

delphi - TEventObject 和 WebBrowser

delphi - Delphi 7 和 Delphi 2007 中接口(interface)的区别

delphi - 无法使用 SendInput(Edit1.Text) 复制俄语(西里尔字母或 Unicode)符号

delphi - TAdoQuery.ParseSql 在 xe4 中不起作用

delphi - 将光标置于错误的 Tedit 文本 Delphi

delphi - Delphi 中重复直到中使用的函数 Sleep() 的奇怪行为

delphi - FPC指针操作到Delphi调整器

function - 在函数外部声明数组会提高重复调用函数的性能吗?

delphi - Win32 中的 Delphi SOAP 入门

delphi - 最长算术和几何级数序列误差