LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

The ON command

Root / Submissions / [.]

RaichuBenderCreated:
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.

If the value is less than 0 or greater than the number of labels, it just skips the command IIRC. so for the between 2 and 4 example you could just do ON A%-2 GOSUB @ROUTINE,@ROUTINE,@ROUTINE

ON only works with labels. If only... EDIT: also you spelled ELSEIF wrong (I noticed because I apply syntax highlighter to icode)

Replying to:snail_
ON only works with labels. If only... EDIT: also you spelled ELSEIF wrong (I noticed because I apply syntax highlighter to icode)
Fixed. Also, how do you do syntax highlight on icode? do you just have siteJS to run my highlighter on all <icode>s?

The IF/ELSEIF block looks more readable IMO. We should do a speed test to see which is faster though. IF ELSEIF vs. ON GOSUB vs. CALL

Replying to:snail_
ON only works with labels. If only... EDIT: also you spelled ELSEIF wrong (I noticed because I apply syntax highlighter to icode)
yes

The problem with ON GOTO/GOSUB is you are using GOTO or GOSUB. You are making things harder to keep track of and relying on global state to pass around variables. It makes maintaining your code A LOT harder. I find the IF/ELSEIF tree nicer although I would have liked SELECT CASE better still. While it is a nice option I ultimately never use it, as I prefer functions to an extreme degree.

Wellp, there were/are some problems with this page... I shouldn't start a pages when I'm at school :P

Replying to:12Me21
The IF/ELSEIF block looks more readable IMO. We should do a speed test to see which is faster though. IF ELSEIF vs. ON GOSUB vs. CALL
Yeah, already did that. Apparently IF and ELSIF is faster. I just like the way how ON GOSUB works, because it's similar to switch statements, which I'm used to using in other programming languages. It's just preference I guess. If I really need to speed things up, I'll eventually replace all ONs and CALLs with IFs and ELSEIFs

Replying to:snail_
ON only works with labels. If only... EDIT: also you spelled ELSEIF wrong (I noticed because I apply syntax highlighter to icode)
Did I spell it like ELIF? I'm used to Python, so I confuse them sometimes...