Discussion:
Break out of loop in TP 5.5
(too old to reply)
Sm704
2005-04-18 21:45:06 UTC
Permalink
How 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.

Thanks.
Brian
2005-04-18 22:04:41 UTC
Permalink
Post by Sm704
How 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.
Restructure using while/do or repeat/until?


B.
--
Now playing: "Chicken strings a-gogo"
Bill Leary
2005-04-18 23:46:22 UTC
Permalink
Post by Sm704
How 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;
Done:
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.

- Bill
F Verbeek
2005-04-20 01:41:00 UTC
Permalink
In the obscure news:hOSdnaNMHN3U2PnfRVn-***@giganews.com,
Someone calling himself Bill Leary
Post by Bill Leary
Post by Sm704
How 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.
Alternatively one can use Exit to jump out of the procedure or function.

procedure helloworld;
begin
repeat
case upcase(readkey) of
'H': write('Hello ');
'W':writeln('world')
'#27':exit;
until false;
end;


--
Femme Verbeek
Bill Leary
2005-04-20 04:39:05 UTC
Permalink
Post by F Verbeek
Someone 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 Verbeek
Post by Bill Leary
Post by Sm704
How 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 Verbeek
Alternatively 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 Verbeek
procedure 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
Dr John Stockton
2005-04-20 22:37:57 UTC
Permalink
REPOST since original post apparently gone AWOL.
Post by Sm704
How 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.
You can make the loop the final entity in a function or procedure, and
use EXIT.

You can use a repeat-until loop :-

finished := false ;
repeat
...
...
if IWantToBreak then finished := true else begin
...
...
end ;
until finished ;
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
Loading...