Post by F VerbeekSomeone calling himself Bill Leary
If using my real name as both my identity and my e-mail is "suspiciously hiding"
I wonder what it is when someone uses a handle and an invalid e-mail.
Post by F VerbeekPost by Bill LearyPost by Sm704How can I break out of a loop in Turbo Pascal v5.5? In TP 7.0, you
use the "break" command, but in 5.5, the compiler says it's an
Unknown identifier.
Hearking back to our ancient knowledge, you could use GOTO.
{ Turbo Pascal 5.5 "exit loop early" test }
Program Test;
Var i: Integer;
Label Done;
Begin
WriteLn('Starting');
For I := 1 To 10 Do
Begin
WriteLn(I);
If I = 5 Then
Goto Done;
End;
WriteLn('Ending')
End.
Of course, some will be making the Sign of Protection vs. Evil at
this. And truly, in most cases, restructuring the loop would be the
better answer. But this works.
Yes, ugly indeed, but so is break.
Depending on what you're doing, the GOTO might actually be more readable in that
it may make it easier to find where you ended up after exiting the loop. And,
at least within Turbo's, it's the more portable construct.
As may be, I'd try to find a way to avoid either if I could. But in some cases
restucturing to avoid the Goto or Break could be even less clear than just using
one of them.
Post by F VerbeekAlternatively one can use Exit to jump out of the procedure or function.
Sure, but the OP asked how to break out of the loop. I assumed there were
things to be done after exiting the loop. Thus, I put in the WriteLn('Ending');
after it to ensure that the following code was executed.
Post by F Verbeekprocedure helloworld;
begin
repeat
case upcase(readkey) of
'H': write('Hello ');
'W':writeln('world')
'#27':exit;
until false;
end;
This'll work, but you'd have to put the loop into a function or procedure and
invoke it in place of your loop so when it EXITs you can do whatever you were
going to do after breaking out of the loop. This could be an example of making
the program less clear in order to avoid Goto or Break. Or maybe not...
{ Turbo Pascal 5.5 "exit loop early using Exit" test }
Program Test;
Var i: Integer;
Label Done;
Procedure EarlyExit(X: Integer);
Begin
For I := 1 To X Do
Begin
WriteLn(I);
If I = 5 Then
Exit;
End;
WriteLn('Didn''t exit early')
End;
Begin
Writeln('Starting');
EarlyExit(10);
WriteLn('Ending')
End.
This gets exactly the same output as my earlier example, and avoids a Goto or
Break. But it does mean the loop is no longer right where you can see it. One
could even turn this into an advantage, if the procedure name really brought out
what was going on.
- Bill