Small Basic Forum

Full Version: How to exit a subroutine half way through
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there:

Just not sure if this is possible with small basic.

Been trying to exit a subroutine half way through but so far does not have much success, any ideas ?

All help is much appreciated.

Regards
(translated by Google translator)

Hello, Yumda.  Shy

At the very end of the subroutine, I insert some necessary line of code or an empty operation, like this: "x = x".
Before this line I insert a transition label, for example: "subName_Exit:".

Now I can jump to the end of the subroutine from anywhere in the code to exit it.

' ---
Sub Test
  ...  ' any code

  Goto subTest_Exit

   ... ' any code
  subTest_Exit:
  x = x
EndSub
' ----
Either Goto as AB showed (don't need the x=x though) or encase unwanted code in If ("False").  Note you must keep the GoTo jump within the same subroutine.

Code:
Test1()
Test2()

Sub Test1
  TextWindow.WriteLine("Used code 1")
  Goto subTest_Exit
  TextWindow.WriteLine("Unused code 1")
  subTest_Exit:
EndSub

Sub Test2
  TextWindow.WriteLine("Used code 2")
  If ("False") Then
    TextWindow.WriteLine("Unused code 2")
  EndIf
EndSub