ON: Your Multi-Path Maestro
Tired of simple IF...THEN
choices? Want your Commodore 64 programs to branch out in multiple directions based on a single value? Enter the ON
command, your multi-path maestro! This versatile tool lets you evaluate an expression and then jump to different line numbers depending on its value. It's like having a switchboard operator for your code, directing traffic to various destinations!
Syntax
ON <expression> GOTO <line1>[, <line2>, ... , <lineN>]
or
ON <expression> GOSUB <line1>[, <line2>, ... , <lineN>]
Where:
- <expression>
: A numeric expression that will be evaluated.
- <line1>, <line2>, ... , <lineN>
: A list of line numbers. The program will jump to the line number corresponding to the value of the expression. For example, if the expression evaluates to 3, the program will jump to <line3>
.
Applications
The ON
command opens up a world of possibilities:
- Menu Systems: Create menus where the user's choice (1, 2, 3, etc.) directly corresponds to a specific subroutine or section of code.
- Error Handling: Jump to different error handling routines based on the error code.
- State Machines: Create programs that transition between different states depending on a numeric input or condition.
- Game Logic: Control character actions, level selection, or other branching behaviors based on numerical values.
Code Examples
1. Simple Menu System:
10 PRINT "Menu:"
20 PRINT "1: Option 1"
30 PRINT "2: Option 2"
40 PRINT "3: Option 3"
50 INPUT "Choice: "; C
60 ON C GOSUB 100, 200, 300 :rem Jump to the appropriate line based on choice
70 END
100 PRINT "You chose option 1" :rem Subroutine for option 1
110 RETURN
200 PRINT "You chose option 2" :rem Subroutine for option 2
210 RETURN
300 PRINT "You chose option 3" :rem Subroutine for option 3
310 RETURN
ON in the Wild: The Choose-Your-Own-Adventure Creator
Imagine you're writing a text-based adventure game. The ON
command can be your guide, allowing players to make numerical choices that lead them down different paths in the story, creating a personalized and interactive experience.
Don't be limited to a single path! With the ON
command, you can create branching code that adapts to different situations and user inputs. It's a powerful tool that adds flexibility and structure to your Commodore 64 programs. So go ahead and explore the possibilities - let ON
be your guide to a world of multiple choices!