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?
continue with while LoopIn a while loop, continue skips the current iteration and control flow of the program jumps back to the while condition. The continue statement works in the same way for while and do... while loops.
The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.
Java continue statement is used to skip the current iteration of a loop. Continue statement in java can be used with for , while and do-while loop.
The continue statement can be used with any other loop also like while or do while in a similar way as it is used with for loop above.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With