7-4 Programming
Sequence
Trio Motion Technology
Sequence
The ability to process a series of instructions, in a logical order, and to
control the flow by branching to another part of the program.
Normally, a program executes statements in sequence starting at the top. In
order to branch between different sections of the program we need to be able
to identify specific sections of the code. Labels are used as place markers to
indicate the start of a routine, or the target for the ‘branch’ instructions,
GOTO
and
GOSUB
.
It is useful to split your program up into a series of routines, each of which
handles a particular funtion of the machine. The
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. Take the following example:
PRINT "Hello"
GOSUB a_subroutine
STOP
a_subroutine:
PRINT "World"
RETURN
The program will print the "Hello" text to the terminal window, then jump to
the line of the program labelled
‘a_subroutine’
and continue execution. The
next command it finds will print "World". The
RETURN
command then returns
the program to the point it left, where it then proceeds onto the next
command after the
GOSUB
command which in this case is the
STOP
command,
which halts the execution of the program.
The
GOTO
command does not remember where it jumped from and will
continue running from its new location permanently. This might be used for
example, if we have a certain process which needs to be performed when
shutting down a machine, we might jump directly to that routine:
i.e.
GOTO shut_down
Trio BASIC instructions:
Labels, GOTO, GOSUB, RETURN, STOP