Re: Exiting multiple loop constructs

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

> On 13 May 2008 at 18:58, Jeremy Cowgar wrote (maybe snipped):

> 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, the last method is obviously the best as it provides 
additional flow control. Alas, I still in doubt about "exit" keyword 
behavior. I'm sorry to insist, but maybe I should exemplify.

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
            elseif j = 7 then
+------------<- continue top
|           end if
|
|           -- exit all loops (in this function) 
|           if j = 5 then exit all end if ->+
|        end for                            |
|   end for                                 |
+-> ...                                     |
end while                                   |
... <---------------------------------------+


Did I get it right or should I go after an ice-cream cone to stick it 
in the head? :P

Best,
Euler

-- 
_
_| euler f german
_| sete lagoas, mg, brazil
_| efgerman{AT}gmail{DOT}com

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

Search



Quick Links

User menu

Not signed in.

Misc Menu