Re: Exiting multiple loop constructs

new topic     » goto parent     » topic index » view thread      » older message » newer message

I reject the 'numbered' loop approach due to the added work it requires during
maintenance.

> The final method is labeled loops, in which I will make a slightly more
> advanced
> example.
> 
> }}}
<eucode>
> while 1 label top do
>     for i = 1 to 10 label mid do
>         for j = 1 to 10 label bottom do
>             -- exit current loop "to" the top loop
>             if j = 2 then exit top end if
> 
>             -- exit all loops (in this function) 
>             if j = 5 then exit all end if
>         end for
>     end for
> end while
> </eucode>
{{{


You seem to be saying that 'exit' means 'exit TO' rather than the existing
meaning of 'exit FROM'.

To me, the label identifies a loop and the 'exit <name>' construct means 'stop
executing code in the loop called <name> and commence at the next statement after
the loop's "end" statement' and it should not mean 'proceed immediately to the
start of the loop called <name> and increment the counter'. That is the meaning
of the 'next' keyword.

In short ... 
... 'exit' should mean EXIT FROM the (named) loop.

... 'next' should mean begin the NEXT iteration of the (named) loop.

... 'retry' should mean RETRY the same iteration of the (named) loop. 

So I would rewrite your eaxmple as ...
while 1 label top do
    for i = 1 to 10 label mid do
        for j = 1 to 10 label bottom do
            -- stop processing current loop and go to the top loop
            if j = 2 then next top end if

            -- exit current loop and go to end of mid loop
            if j = 4 then exit mid end if

            -- exit all loops (in this function) 
            if j = 5 then exit all end if
        end for
    end for
end while



> Should any syntax change to support exiting multiple loops?

Huh? Do you mean exiting from deeply nested loops or are you referring to
something else. In any case the 'exit all' phrase is a bit redundant because, as
per your example, one could code 'exit top' and the topmost loop is labelled.

I'm thinking that the 'label' syntax is just too easy to overlook as it makes
the code seem cluttered. Maybe using some punctuation might help make it stand
out?

while 1 :top: do
    for i = 1 to 10 :mid: do
        for j = 1 to 10 :bottom: do


while 1 @top do
    for i = 1 to 10 @mid do
        for j = 1 to 10 @bottom do


I do think that the label needs to be to the left of the 'do' but maybe it can
actually be to the left of the loop statement.
@top while 1 do
    @mid for i = 1 to 10 do
        @bottom for j = 1 to 10 do


because then it could be made to standout even more by placing it on its own
line ...

@top
while 1 do
    @mid
    for i = 1 to 10 do
        @bottom
        for j = 1 to 10 do


-- 
Derek Parnell
Melbourne, Australia
Skype name: derek.j.parnell

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu