Branching

There's a number of other options available for controlling the flow of your program alongside functions including gosub and goto. These commands are present for completeness and are not recommended to be used. They date back to the early days of the BASIC language before functions existed.

Normally, a program executes statements in sequence starting at the top. A branch statement allows you to jump to another part of the program to continue execution. A gosub command will jump to a label and continue from its new location. When the program encounters a return command, the program will jump back to the gosub from where it originally came. Here's an example:

do
	gosub myGosub

sync ( ) loop
myGosub: print ( "inside the gosub" ) return

This program behaves in a similar way to a function, except there's no parameters or return value. If the return keyword was not used the program would continue to execute any code that is beneath it, in our case nothing, so the program would simply end.

The goto command is similar in behaviour to a gosub, except this does not remember the location where it came from. The example that follows demonstrate how it's possible to jump around the program.

partA:
do
	print ( "inside a do loop and partA" )
	goto partB
	print ( "this code will never be executed" )
loop

partB: print ( "inside partB" ) sync ( ) goto partA

The commands to branch around the program can be useful, albeit in very limited circumstances. In the main it's advisable to stay away from them and instead control the flow of your program using functions.