Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exiting sub while inside a loop
#1
Is it "kosher" to exit a sub while inside a loop in the sub, or does it create problems (e.g. stack corruption etc.)?

(no, Pete, I don't mean a submarine)!
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#2
It's fine.  The loop should be in the sub's scope.  When you exit the sub everything is cleared except for static variables.  If you do it with a function make sure you set the return value first.
Reply
#3
Caveat: If you have settings to restore, handles to free, mem to deallocate, you may just want to GOTO the cleanup portion of your sub and exit normally, rather than breakout with an EXIT SUB and skip all that.
Reply
#4
(12-05-2025, 03:36 AM)SMcNeill Wrote: Caveat: If you have settings to restore, handles to free, mem to deallocate, you may just want to GOTO the cleanup portion of your sub and exit normally, rather than breakout with an EXIT SUB and skip all that.

Ok, thanks both. Wasn't sure if it left something smoldering, that may come back to bite me later.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#5
It won't cause a stack space issue if FOR/NEXT or DO/LOOP WHILE/WEND. Stack issue are a result of doing a gosub without a return. GOTO statements within the gosub routine are the usual culprits, and its just bad coding practice to say the least.

I try to limit using EXIT SUB. Granted I need one when the bottom of a subroutine has one or more gosub routines in it.We have to exit before the program flow hits the gosub routine on its way to the end sub statement.

A way to accomplish the same thing as EXIT SUB is a double loop.

Sub Steve
    While -1
        Do
            If Pete = Tremendous Then Exit While
        Loop
        Rem Blah, blah, blah...
    Wend
End Sub
Shoot first and shoot people who ask questions, later.
Reply
#6
(12-05-2025, 08:24 AM)Pete Wrote: It won't cause a stack space issue if FOR/NEXT or DO/LOOP WHILE/WEND. Stack issue are a result of doing a gosub without a return. GOTO statements within the gosub routine are the usual culprits, and its just bad coding practice to say the least.

I try to limit using EXIT SUB. Granted I need one when the bottom of a subroutine has one or more gosub routines in it.We have to exit before the program flow hits the gosub routine on its way to the end sub statement.

A way to accomplish the same thing as EXIT SUB is a double loop.

Sub Steve
    While -1
        Do
            If Pete = Tremendous Then Exit While
        Loop
        Rem Blah, blah, blah...
    Wend
End Sub

Thanks Pete. Very useful info.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Do Loop, Sleep and Mouse Button Dimster 5 578 09-06-2025, 12:57 PM
Last Post: Dimster
  Using modulo to loop through lists fistfullofnails 3 705 09-03-2025, 11:50 PM
Last Post: fistfullofnails
  What is wrong with this for/next loop Helium5793 6 1,120 04-15-2025, 05:11 PM
Last Post: Kernelpanic
  Question on ln in a for/next loop Dimster 13 2,180 09-13-2024, 11:07 PM
Last Post: Kernelpanic
Photo Flickering of the screen due to images and text in a loop Delsus 8 1,473 09-08-2024, 07:18 AM
Last Post: Delsus

Forum Jump:


Users browsing this thread: 1 Guest(s)