Using Continue in a While loop
Asked Answered
S

2

7

Can the Continue be used in a While loop when working with text files?

I would like to do some processing and check some values. If true, i would like to skip a iteration. If false, i would like to continue with the next set of lines (continue processing).

while not EOF(InFile) do  
begin  
 DoSomething;  
 if (AcctTag = '') OR (MasterId = '') then  
  Continue;  
 DoSomething;  
end;  

Does the continue in this case skip a iteration?

Supernational answered 30/8, 2011 at 0:36 Comment(3)
I sometimes find it makes code clearer if you can skip continue. In your case: If (AcctTag <> '') And (MasterId <> '') Then DoSomething;Surrebuttal
@johnny, fully agree, continue is like a goto, use it with extreme care or face the wrath of the spaghetti code monster.Inflexion
if (AcctTag = '') OR (MasterId = '') then Next;December
B
10

A test isn't even necessary. The documentation already tells you the answer:

In Delphi code, the Continue procedure causes the flow of control to proceed to the next iteration of the enclosing for, while, or repeat statement.

Notice that there are no caveats about what the loop is doing. The Continue statement proceeds to the next iteration of any loop. In your case, that means Eof will be checked again, and then the body of the loop will run.

Budget answered 30/8, 2011 at 13:57 Comment(1)
I think where there is ambiguity is in the meaning of "the next iteration". What precisely does this mean for for, while and repeat? I do know the answers to this question, but the documentation would be better if it spelled it out, in my view.Underlinen
W
13

Seems a quick 30-second test would answer that more quickly than a post here. :)

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

Note that i is incremented before the call to Continue, which results in j displaying odd numbers, i displaying even? j is only incremented when the loop goes past the Continue test.

A while works the same way whether you're incrementing an integer, concatenating a string, or reading from a text file. A while is a while is a while no matter how you use it. You just need to make sure, in your code above, that DoSomething actually reads the next line from the file or you'll end up in a continuous loop.

Weichsel answered 30/8, 2011 at 0:48 Comment(1)
Moderator Note Please keep comments constructive and on topic. I've removed comments under this answer because they degenerated into noise.Costanzia
B
10

A test isn't even necessary. The documentation already tells you the answer:

In Delphi code, the Continue procedure causes the flow of control to proceed to the next iteration of the enclosing for, while, or repeat statement.

Notice that there are no caveats about what the loop is doing. The Continue statement proceeds to the next iteration of any loop. In your case, that means Eof will be checked again, and then the body of the loop will run.

Budget answered 30/8, 2011 at 13:57 Comment(1)
I think where there is ambiguity is in the meaning of "the next iteration". What precisely does this mean for for, while and repeat? I do know the answers to this question, but the documentation would be better if it spelled it out, in my view.Underlinen

© 2022 - 2024 — McMap. All rights reserved.