this post is sort of a response to
http://www.network54.com/Forum/13959/message/1198000416/using+goto+exit+a+while-wend
and its replies. As the thread that's in won't be on the first page for much longer, I decided to make a new thread.
DEFINT A-Z
CLS
GOTO 1
DO
PRINT "you will see this (1st DO test)"
IF LEN(INKEY$) THEN GOTO 3
2 LOOP
1 DO
PRINT "you will see this only once (1st DO test)": SLEEP 1
GOTO 2
LOOP
3 DO
PRINT "you will see this only once (2nd DO test)": SLEEP 1
GOTO 4
LOOP
DO
PRINT "you will see this (2nd DO test)"
IF LEN(INKEY$) THEN GOTO 5
4 LOOP
WHILE -1
PRINT "you will see this (1st WHILE test)"
IF LEN(INKEY$) THEN GOTO 7
6 WEND
5 WHILE -1
PRINT "you will see this only once (1st WHILE test)": SLEEP 1
GOTO 6
WEND
7 WHILE -1
PRINT "you will see this only once (2nd WHILE test)": SLEEP 1
GOTO 8
WEND
WHILE -1
PRINT "you will see this (2nd WHILE test)"
IF LEN(INKEY$) THEN GOTO 9
8 WEND
FOR i = 0 TO 7
PRINT "you will see not this (1st FOR test)"
10 NEXT i
PRINT "you will see this also (1st FOR test)"
IF LEN(INKEY$) THEN GOTO 11
9 FOR i = 0 TO 7
PRINT "you will see this (1st FOR test)"
GOTO 10
NEXT i
11 FOR i = 0 TO 7
PRINT "you will see this only once (2nd FOR test)"
GOTO 12
NEXT i
FOR i = 0 TO 7
PRINT "you will not see this (2nd FOR test)"
12 NEXT i
PRINT "you will see this only once also (2nd FOR test)": SLEEP 1
PRINT
PRINT
PRINT "conlcusions: the LOOP and WEND statements are hard coded to return to specific";
PRINT "DO and WHILE statements. Upon exection, they can jump back to the DO or WHILE"
PRINT "even if that particular DO or WHILE was not originally executed."
PRINT
PRINT "the NEXT statement appears smarter, though. If the wrong NEXT statement is"
PRINT "executed, execution just falls through. The NEXT statement also is hardcoded"
PRINT "to a particular FOR statement, but is smart enough to know that that FOR"
PRINT "statement was never executed."
SYSTEM
'SLEEP: WHILE INKEY$ <> "": WEND
So, of the forms I demonstrated in my reply to Raymond, the DO...LOOP example should suffice. The FOR...NEXT should suffice only if there are no jumps into the loop block. The NEXT part needs to track whether the FOR part was executed or not. To do this in a stack safe way, we might use a hidden variable to track whether the FOR was executed.
for a = 0 to 8 step 2
goto outfor
gotodest: 'destination of some other GOTO
next a
outfor:
could be, as an example:
mov word [_a],0x0
mov byte [for0000enabled],0xff
for0000
jmp short _outfor
_gotodest
test byte [for0000enabled],0xff
jz for0000out
add word [_a],0x2
cmp word [_a],0x8
jl for0000 ;if _a is signed
for0000out
mov byte [for0000enabled],0x0 ;after for0000out in case of EXIT FOR
_outfor
obviously, in the case of non-STAIC SUBs and FUNCTIONs, the variables or pointers, including the for0000enabled variable, would be on the stack as offsets from BP as opposed to being actual symbols.
This brings up the question of what to do about STATIC SUBs that might be subject to recursion.
Comments are welcome.
Regards, Michael
P.S. Some of the conclusions of this post are untrue. See the first child post for an example.