QUITTING from 2 NESTED REPEAT loops, when the inside loop quits?
DavidM
Posts: 630
Hi,
I have two nested repeat loops
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..
Any Ideas if this is possible without another variable?
Thanks
Dave M
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
Post Edited (Paul G) : 8/4/2010 8:29:16 AM GMT
Also, your
should probably read
or simply
The way it's now it will always evaluate to TRUE.
Post Edited (kuroneko) : 8/4/2010 8:32:29 AM GMT
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
Hope this helps [noparse]:D[/noparse]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://www.propgfx.co.uk/forum/·home of the PropGFX Lite
·
That's it!!!
I do have this as its own METHOD so that idea of yours should work!
Thanks
Dave M
However, even in this case there is a dirty solution. Simply change the index variable of the outer loop to trick the loop condition:
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
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 )
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Airspace V - international hangar flying!
www.airspace-v.com/ggadgets for tools & toys