Nested Repeat loops

Warren Samples warren at warrensweb.us
Fri Jun 20 23:21:18 EDT 2014


On 06/20/2014 08:18 PM, Peter Haworth wrote:
> Wondering if anyone has an elegant way of exiting all the way out of a set
> of nested repeat loops., e.g:
>
> repeat for...
>     repeat for...
>        repeat for....
>           repeat for
>              if ..... then <I want to exit out of the outermost repeat loop
> here>
>           end repeat
>           <do this and that>
>        end repeat
>        <do this and that>
>     end repeat
>     <do this and that>
> end repeat
>
> Right now, I set a flag to true when the exit condition is met then test it
> in the <do this and that> stuff.  Works fine but feels a little kludgy.
>
> Pete

Maybe I've completely failed to understand what it is you don't like 
about your current method, but...

Would it be practical and feel cleaner to wrap your nested repeats 
inside a command, say,'doLoop'. When your 'if' condition in the nth 
repeat is met, you 'exit doLoop'. You only have to reference and check a 
condition once this way and you cleanly exit the entire nested structure 
exactly when and where the condition is found.

A useless example that works:

Create a card with four fields and a button. Put this script in the button:


  on mouseUp
    doLoop
    answer "exited loop"
end mouseUp

on doLoop
    repeat 10 times
       add 1 to field 1
       repeat 10 times
          add 1 to field 2
          repeat 10 times
             add 1 to field 3
             repeat 10 times
                if field 3 is 3 then exit doLoop
                add 1 to field 4
             end repeat
          end repeat
       end repeat
    end repeat
end doLoop

Warren




More information about the Use-livecode mailing list