Chapter 2 of 12
Choose a destination
A good pilot can change their mind. A good menu can survive a slip of the finger. Let’s type the wrong answer on purpose and see whether the flight computer gives us another chance.
Fly the menu
Open HELM.bas from the Manual Library. It opens an editable example. In a browser, save the file and open it with Develop → BASIC Development… → Open…. Choose the editor’s Run. If you want to replace the practice listing from chapter 1, confirm. Click the CoCo display.
- At SET COURSE, enter X and press Return. Look for TRY 1 OR 2 and another question.
- Enter 1 and press Return. Look for BOUND FOR EARTH and
OK. - Run again and enter 2. Now you’re BOUND FOR MARS.
Here is the entire helm. You already know the two lines that print the menu.
10 WIDTH 40:CLS
20 PRINT "PLANET EXPLORER"
30 PRINT "1 EARTH 2 MARS"
40 INPUT "SET COURSE";C$
50 IF C$="1" THEN P$="EARTH":GOTO 90
60 IF C$="2" THEN P$="MARS":GOTO 90
70 PRINT "TRY 1 OR 2"
80 GOTO 40
90 PRINT "BOUND FOR ";P$
100 END
Follow the choice with your finger
WIDTH 40 puts the CoCo 3 in its 40-column text mode. The colon joins two instructions on one numbered line. CLS clears the screen before the sign goes up.
On line 40, INPUT prints a prompt and waits for a reply followed by Return. C$ is a variable: BASIC remembers a value under that name. The dollar sign says its value is a string. When you type 2, BASIC stores the character “2”. That’s why the comparison on line 60 has quotes around the digit. A room number or a menu choice can be a perfectly good piece of text.
Put one finger on line 50. The equal sign in IF C$="1" asks a question. If the answer is true, the part after THEN assigns a name to P$ and sends execution to line 90. If it’s false, BASIC skips the rest of line 50 and tries line 60. The equal sign in P$="MARS" has another job: it puts a value into a variable. Watch for whether you’re reading a question or an assignment.
| Reply | Lines after INPUT | Why |
|---|---|---|
| 1 | 50 → 90 → 100 | The first comparison succeeds and the jump skips the rest of the menu. |
| 2 | 50 → 60 → 90 → 100 | The second comparison succeeds. |
| X | 50 → 60 → 70 → 80 → 40 | Both comparisons fail. The program prints a hint and jumps back to INPUT. |
The semicolon in line 90 prints two things side by side. The space at the end of the quoted words leaves room before the planet name. An extra menu trip costs almost nothing when all the program has to do is jump back a few lines.
To try the same decision in another language, give Mars a key in C. That version reads one keystroke as soon as it arrives. To see where the processor keeps the chosen color and message, catch the two assembly routes where they meet.
Ask a question about the answer
After the Mars run, stay at the guest’s OK prompt. Your listing has ended, but P$ still holds the name from that flight. Try these immediate commands one at a time.
PRINT LEN(P$)
PRINT LEFT$(P$,1)
PRINT RIGHT$(P$,2)
You should get 4, M, and RS. A function takes a value and produces a result. LEN counts characters. LEFT$ and RIGHT$ give you pieces of the original string. None of those questions changed the name. In the chart-data lesson, BASIC will keep several names at the same time.