I have seen a lot of people using
IFs,
ELSEs and
ELSEIFs, where the
ON command could be used.What I mean is the following:
VAR A% = RND(4)
IF !A% THEN
'Execute this...
ELSEIF A%==1 THEN
'Execute that...
ELSEIF '...
You get the idea.
Did you know you can make the code more readable by using the
ON command? It works like this:
VAR A% = RND(4)
ON A% GOSUB @ZERO, @ONE, @TWO, @THREE
@ZERO
'Execute this...
RETURN
@ONE
'Execute that...
RETURN
@TWO
'...
First you define a variable and let it be whatever value you want, then you create some subroutines and call them using the
ON command and
GOTO/GOSUB depending on the content of the variable. In the example above I made the variable
A% to be a random value, but you can do lots more. It's similar to using switch statements in other programing languages.
Syntax:
ON <variable> GOTO|GOSUB @<label if variable == 0>,@<label if variable == 1> 'etc.
But if, for example you want a routine to be called if your value is between 2 and 4. You can do it like this:
ON A%-2 GOSUB @ROUTINE,@ROUTINE,@ROUTINE.
A problem with this method is that it only works with integers. The other limitation is that it only works with
@LABELS and not with
DEF-blocks.
I use this a lot for my GameBoy emulator, since IFs became the bane of my existance.
If I missed something that you feel needs to be added, let me know below.