Re: Exiting multiple loop constructs

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

Jeremy Cowgar wrote:
> 
> We've talked about labeled loops and a bit about existing loop constructs,
> exiting
> loops, etc... but I do not think we came to any conclusive decision. Here is
> a summary of different methods of exiting multiple loops:
> 
> }}}
<eucode>
> integer in_top
> in_top = 1
> 
> while in_top do
>     for i = 1 to 10 do
>         if i = 3 then
>             in_top = 0
>             exit
>         end if
>     end for
> end while
> </eucode>
{{{

> 
> This will work fine unless you have processing *under* the nested loop, then
> you must provide an extra condition check (which is in a loop and is executed
> each iteration):
> 
> }}}
<eucode>
> integer in_top
> in_top = 1
> 
> while in_top do
>     for i = 1 to 10 do
>         if i = 3 then
>             in_top = 0
>             exit
>         end if
>     end for
>     if in_top = 0 then
>         exit
>     end if
>     -- do more processing here
> end while
> </eucode>
{{{

> 
> NOTE: The above two methods compound in complexity with each nested loop you
> add. However, in real life, I'm not sure if I have ever programmed a loop that
> is 3 deep.
> 
> This method uses optional numeric methods to exit and continue keywords:
> 
> }}}
<eucode>
> while 1 do
>     for i = 1 to 10 do
>         if i = 3 then exit 2 -- end if
>     end for
> end while
> </eucode>
{{{

> 
> Notice "exit 2". That means exit 2 loops.
> 
> 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>
{{{

> 
> 
> So. Is one clearer than another? I do not know. Should any syntax change to
> support exiting multiple loops? I am not sure, the benefit is you can remove
> multiple checks making code easier to write and better performing.
> 
> So, what do you think? Are there other ways that I missed? What should be
> done?
> 
> --
> Jeremy Cowgar
> <a href="http://jeremy.cowgar.com">http://jeremy.cowgar.com</a>

There is another way:
procedure nested_loops(sequence args)
for i=1 to 27 do
  while f(x)=3 do
    ....
         if exit_em_all() then
             return
         end if
end end.... end
end procedure

-- ... some code
nested_loops(args)

and after all, nesd_loops() may be a function as well.

While this works well at top leel, it is less obvious in a routine, beause
nested_loops() may access the privates of the calling routines.

If, like Æ or Pascal, Euphoria had nested routines, it would work fine. And the
chagnges are not that difficult really.

The method you quote first is what e are doing now. Error prone, invites a goto
statement to make the code more maintainable.

Method #2 is good in small programs, the code is the cleaest, but maintaining is
tricky because ading/removing a loop level will cause to examine all
exit/continue statements.

Method #3 is more heavyhanded, but after all, not all loops are affected, and
maintaining is the easiest.

Methood #4 has been available, but does not always work, or becomes as error
prone as #1 when privates have to be passed and updated.

My suggestion would be to add method #3, possibly adding #2. For #2, things can
be made less fragile by being able to use non positive optional arguments - exit
0 would exit all loops, exit -1 all but the topmost, etc. Like when using $ for
sequences.

Alternatively, if we had nested routines, we'd solve a few more problems and
could always use method #4. Perhaps the best.

CChris

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

Search



Quick Links

User menu

Not signed in.

Misc Menu