Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exit Sub from inner loop?
#8
(02-03-2023, 12:04 AM)SMcNeill Wrote: Aye.  As I mentioned previously, exiting from a SUB has zero bad repercussions.


Unless...

Code: (Select All)
SUB foo
   FOR i = 1 to 100
      GOSUB fooGosub
   NEXT
   EXIT SUB

fooGosub:
    PRINT i
    IF i = 10 THEN EXIT SUB
    RETURN
END SUB

Now, in the above, you're going to have issues eventually, and some folks would say "SEE?!!  YOUR EXIT SUB EXPLODED THE PROGRAM!!!" -- which isn't *actually* the issue.  The problem is you used an EXIT SUB to exit the sub WITHOUT returning from the GOSUB first.  It's the GOSUB and the lost RETURN which is the true problem, and not the EXIT SUB, but it's still something to know to watch out for, if you ever use the routines in combination like that.  Wink

Sorry, Pete, I think I missed the point (yes, I'm a bit dense), but I ran your sub foo 10000 times (from a loop) and it executed as I expected - counted and displayed 1 to 10, then, from my loop, repeated this flawlessly and without throwing an error.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply


Messages In This Thread
Exit Sub from inner loop? - by PhilOfPerth - 02-02-2023, 09:32 AM
RE: Exit Sub from inner loop? - by SMcNeill - 02-02-2023, 09:43 AM
RE: Exit Sub from inner loop? - by PhilOfPerth - 02-02-2023, 10:39 AM
RE: Exit Sub from inner loop? - by OldMoses - 02-02-2023, 04:10 PM
RE: Exit Sub from inner loop? - by PhilOfPerth - 02-02-2023, 11:09 PM
RE: Exit Sub from inner loop? - by PhilOfPerth - 02-02-2023, 11:48 PM
RE: Exit Sub from inner loop? - by SMcNeill - 02-03-2023, 12:04 AM
RE: Exit Sub from inner loop? - by PhilOfPerth - 02-03-2023, 12:56 AM
RE: Exit Sub from inner loop? - by SMcNeill - 02-03-2023, 02:13 AM
RE: Exit Sub from inner loop? - by PhilOfPerth - 02-03-2023, 02:40 AM



Users browsing this thread: 3 Guest(s)