QB64 Phoenix Edition
Exiting a FOR/NEXt loop - Printable Version

+- QB64 Phoenix Edition (https://staging.qb64phoenix.com)
+-- Forum: Chatting and Socializing (https://staging.qb64phoenix.com/forumdisplay.php?fid=11)
+--- Forum: General Discussion (https://staging.qb64phoenix.com/forumdisplay.php?fid=2)
+--- Thread: Exiting a FOR/NEXt loop (/showthread.php?tid=806)



Exiting a FOR/NEXt loop - Circlotron - 08-24-2022

Hi all. First post. I did post a couple of times on Facebook, but now I'm here.
Okay... I have a FOR/NEXT loop that I want to exit if a certain condition occurs. The thing is, if I put a label for it to go to it doesn't do that. It simply resumes on the next line after the loop. I'm using version 2.02

Code: (Select All)
Cls
For x& = 1 To 10
    Print x&
    If x& = 5 Then Exit For: GoTo stuff
Next x&

Print "done"
here: GoTo here

stuff:
Print "exited loop"



RE: Exiting a FOR/NEXt loop - SMcNeill - 08-24-2022

You EXIT FOR, which moves the program flow at that point to after the NEXT statement. The GOTO never executes.

For x& = 1 To 10
Print x&
If x& = 5 Then Exit For: GoTo stuff
Next x&

PRINT x& ' As you can see here, x& is 5, so you did indeed EXIT the FOR loop.
Print "done"


RE: Exiting a FOR/NEXt loop - Circlotron - 08-24-2022

It certainly did exit the loop. I'm just surprised that it didn't treat the GOTO following the colon as the next instruction and follow it instead. Just now I tried it on QBASIC and it does the same thing. Oh well...


RE: Exiting a FOR/NEXt loop - mnrvovrfc - 08-24-2022

(08-24-2022, 06:17 AM)Circlotron Wrote: It certainly did exit the loop. I'm just surprised that it didn't treat the GOTO following the colon as the next instruction and follow it instead. Just now I tried it on QBASIC and it does the same thing. Oh well...
This is what you expected the program to do:
Code: (Select All)
Cls
For x& = 1 To 10
    Print x&
    If x& = 5 Then Exit For
Next x&

If x& = 5 Then GoTo stuff

Print "done"
here: GoTo here

stuff:
Print "exited loop"
Check out the line right below "next x&"...