Shop OBEX P1 Docs P2 Docs Learn Events
QUITTING from 2 NESTED REPEAT loops, when the inside loop quits? — Parallax Forums

QUITTING from 2 NESTED REPEAT loops, when the inside loop quits?

DavidMDavidM Posts: 630
edited 2010-08-05 06:31 in Propeller 1
Hi,

I have two nested repeat loops

REPEAT X From 1 To 10

  REPEAT Y From 1 To 10

    IF CheckSomething := TRUE

        DoSomething(X,Y)
        QUIT ' Quit from Y loop





If the DoSomething line of code executes I want to QUIT from both loops, So Far I can only QUIT from the Inside (Y loop).

The X or Y values can be any value when the loops need to quit, so I can't monitor these two variables to quit the Outside loop,
So I could only use ANOTHER Local variable ( boolean) that will be set when the dosomething statement is executed.

Like this..

StopOutsideLoop := FALSE

REPEAT X From 1 To 10

  REPEAT Y From 1 To 16

    IF CheckSomething := TRUE

        DoSomething(X,Y)
        StopOutsideLoop == True
        QUIT ' Quit from Y loop

  IF StopOutsideLoop
     QUIT ' Quit the outside loop





Any Ideas if this is possible without another variable?



Thanks
Dave M

Comments

  • Paul GPaul G Posts: 10
    edited 2010-08-04 08:16
    Could you not use 1 loop variable and then modulus and remainder ? Something like this:

    REPEAT x from 0 to 159
    
      IF CheckSomething := TRUE
    
        DoSomething(x/16 +1, x//16+1)
        QUIT
    
    

    Post Edited (Paul G) : 8/4/2010 8:29:16 AM GMT
  • kuronekokuroneko Posts: 3,623
    edited 2010-08-04 08:24
    If it's more about readability then you could try something like this if division and remainder calculations are too expensive (incomplete):

    repeat
      repeat
        active := fn(x, y)
      while --y and active
    while --x and active
    


    Also, your

    IF CheckSomething := TRUE
    


    should probably read

    IF CheckSomething == TRUE
    


    or simply

    IF CheckSomething
    


    The way it's now it will always evaluate to TRUE.

    Post Edited (kuroneko) : 8/4/2010 8:32:29 AM GMT
  • DavidMDavidM Posts: 630
    edited 2010-08-04 08:55
    Hi kuroneko, & paul G

    Interesting suggestions!

    sorry that was a typo, should have been as you said "IF CheckSomething == TRUE"

    Thanks for the suggestions,

    READABILITY is important, I have enough ( hard to read Code as it is) , I like to keep the decision logic easy to read where possible.

    Thanks

    Dave M
  • BaggersBaggers Posts: 3,019
    edited 2010-08-04 10:10
    Quick easy hack is to put it in it's own PUB that way, when you want to exit the routine, you just return [noparse]:D[/noparse]

    PUB quittableroutine
      repeat y from 0 to 99
        repeat x from 0 to 99
          dosomething
          if quitting==1
            return
    
    



    Hope this helps [noparse]:D[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • DavidMDavidM Posts: 630
    edited 2010-08-04 11:54
    Hi Baggers


    That's it!!!

    I do have this as its own METHOD so that idea of yours should work!


    Thanks

    Dave M
  • virtuPICvirtuPIC Posts: 193
    edited 2010-08-04 12:19
    Uh! Ever heard about structured programming? I know, there is no such thing in assembler. But in high level languages it basically means that control flow is not arbitrarily messed up but you only have reasonable constructs that guarantee only to leave one loop level at a time.

    However, even in this case there is a dirty solution. Simply change the index variable of the outer loop to trick the loop condition:

    REPEAT X From 1 To 10 'Value might also be changed inside loop
    
      REPEAT Y From 1 To 10
    
        IF CheckSomething := TRUE
    
            DoSomething(X,Y)
            x := 10
            QUIT ' Quit from Y loop
    
    



    Not really nice since one might expect a repeat-variable only to be changed by loop control. Yes, you should really put a comment at the loop header!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Airspace V - international hangar flying!
    www.airspace-v.com/ggadgets for tools & toys
  • kuronekokuroneko Posts: 3,623
    edited 2010-08-04 12:38
    Baggers said...
    Quick easy hack is to put it in it's own PUB that way, when you want to exit the routine, you just return [noparse]:D[/noparse]
    I was just going to suggest a flavour of abort but a return seems less evil [noparse]:)[/noparse]
  • DavidMDavidM Posts: 630
    edited 2010-08-04 14:07
    Hi VirtualPic,

    Your suggestion to MODIFY X , is what I wirst did to solve the problem , before posting my question here,
    Variable X & Y are passed onto another method, I need to trap the Value of X & Y WHEN the Condition is met, So changing X to 10, stuffs things UP. ( i did this earlier today!)

    Ideally SPIN needs to be able to NEST/INDENT QUIT Commands, i.e 2 x QUIT commands would QUIT 2 REPEAT Loops, Similar to the use of the END statement in other languages.

    But we don't have that, so

    A Simple extra Local variable works well, and most import it is easy to read and understand a year later if I have to visit my code again.

    I have bucket loads DECISION LOGIC in my current application, SPIN is so far doing a good job of it.

    One thing I have learned is to MAKE AS MANY METHODS as you need, ( even if only used once ) to keep the DECISION LOGIC much easier to read, remember all that branching can spread out over more than one screen , so its best to only have the Decision logic and then use SINGLE LINE CALLS to the TASK's ( i.e Methods )

    regards

    Dave M

    PS I am not really familiar with the term STRUCTURED PROGRAMMING , But I am familiar with OOP, I gather that is similar. I have been doing a lot of work in REALBASIC, its an excellent language, a lot of what I have learned their I have tried to apply to SPIN.


    PSS , Sometimes you just have to get code WORKING FIRST ( proof of concept) , and then RE-STRUCTURE/ optimise LATER ( I do this in stages )
  • virtuPICvirtuPIC Posts: 193
    edited 2010-08-05 06:31
    Well, what about making it a single loop like this:
    StopOutsideLoop := FALSE
    
    REPEAT XY FROM 1 TO 160
      IF Y > 16
        x += 1
        y := 1
      IF CheckSomething := TRUE
        DoSomething(X,Y)
        QUIT
        x := y += 1
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Airspace V - international hangar flying!
    www.airspace-v.com/ggadgets for tools & toys
Sign In or Register to comment.