[fpc-devel] Fwd: While - Otherwise Statement

David W Noon david.w.noon at googlemail.com
Tue Oct 13 00:15:31 CEST 2015

Hash: SHA1

On Mon, 12 Oct 2015 15:11:18 -0400, Wkitty42 (wkitty42 at windstream.net)
wrote about "Re: [fpc-devel] Fwd: While - Otherwise Statement" (in
<561C05D6.4010403 at windstream.net>):

> On 10/12/2015 02:02 PM, Dmitry Boyarintsev wrote:
>> The next step would probably be controlled "break", where a user
>> would be able to specify how many nested loops needed to broken
>> from.
> ROTFLMAO! if you need or desire something like that then set a 
> breakcounter and break... in the next outer block, check
> breakcounter to see if you need to break again... if so,
> dec(breakcounter) and break... then check it again in the next
> outer block... and so on and so on until breakcounter=0... why
> should the compiler have to do your logic work for you? ;)

Hi Mark,

You might have seen me write things like this in the old OS2PROG echo
of Fidonet some 20+ years ago. ... :-)

The only language I know that offers that level of control is PL/I,
where the break statement is coded as LEAVE.  It is handled by
labelling the loop control statement and coding the required label in
the LEAVE statement. For example:

   loop_1: DO i = 1 TO m;
      loop_2 : DO j = 1 TO n;
         loop_3: DO k = 1 TO p;
            IF some_condition THEH
               LEAVE loop_3;
            IF another_condition THEH
               LEAVE loop_2;
            IF some_other_condition THEH
               LEAVE loop_1;
         END loop_3;
      END loop_2;
   END loop_1;

If the label is omitted then the immediately containing loop is left.

This allows any of the nested loops to be escaped, potentially all the
way out. I guess the corresponding syntax in a Pascal-esque style
would be:

   loop_1: FOR i := 1 TO m DO
      loop_2: FOR j := 1 TO n DO
         loop_3: FOR k := 1 TO p DO
            IF (some_condition) THEN
               BREAK loop_1;
            IF (another_condition) THEN
               BREAK loop_2;
            IF (some_other_condition) THEN
               BREAK loop_3;

If we're being strictly Pascal, the labels would need to be declared
at the head of the procedure -- and if we're really strict they should
be numeric.
- -- 

Dave  [RLU #314465]
david.w.noon at googlemail.com (David W Noon)

Version: GnuPG v2


More information about the fpc-devel mailing list