MC-10 Operation and Language Reference Manual Chapter 10






10/INTERACTIVE
PROGRAMMING AND THE
MC-10



Up to this point, the programs you've written in Micro Color BASIC have been fairly straightforward. You've given the MC-10 instructions, and the Computer has followed these instructions.

Computers can be somewhat more inquisitive, though. They'll ask you questions-as long as you tell them what questions to ask. (In other words, they're not quite as smart as they'd like you to think!)

The INPUT Command


The Micro Color BASIC INPUT command enables "prompted" input. This means the Computer will ask you a question (the prompt) and you must respond with an answer (the input).

INPUT "message"; variable
message
is the prompting message you want displayed on the TV and is a data string. message must be enclosed in quotes and followed by a semicolon.
variable can be one or more string or numeric variables.


INPUT can be used in Program Mode only. If you use the command in the Immediate Mode, an Illegal Direct Error message will be displayed.

Clear memory by typing NEW [ENTER]. Then, to see how INPUT works, try this example:

    10 CLS
    20 INPUT "WHAT IS YOUR NAME";NAME$
    30 PRINT "HELLO ";NAME$
    40 END

Now RUN the program. Your Screen should display:

    WHAT IS YOUR NAME?

(Micro Color BASIC automatically supplies the question mark since it knows that INPUT implies question.)

Simply type in your name and press [ENTER]. For instance:

    WHAT IS YOUR NAME? JON [ENTER]

When you do so, the TV will change to:

    WHAT IS YOUR NAME? JON
    HELLO JON
    OK

Do you think a little more enthusiasm is required when someone says "hello" to you? Change line 30 to this:

    30 PRINT "HELLO ";NAME$; "!"

INPUT allows you to "bury" variables between string items as long as you separate items with a semicolon.

Numbers can also be used with the INPUT statement. Type NEW [ENTER] and try this:

    10 CLS
    20 INPUT "WHAT IS THE FIRST NUMBER"; A
    30 INPUT "WHAT NUMBER DO YOU WANT TO ADD TO IT"; B
    40 C = A + B
    50 PRINT "THE ANSWER IS "; C
    60 END

Now RUN the program. When Micro Color BASIC asks you what the first number is, type in a number and press [ENTER]. For instance, type 10 [ENTER]. Next, you'll be asked what number you want to add to the first number; type in another number and press [ENTER]. (If you enter anything other than a number you'll be asked to ??REDO the entry.) The answer will then be printed on the TV.

You can also use INPUT to ask more than one question. After typing NEW [ENTER], enter this program:

    10 CLS
    20 INPUT "WHAT'S YOUR NAME AND AGE
       (NAME,AGE)";NAME$,AGE
    25 CLS
    30 PRINT NAME$; " IS"; AGE; " YEARS OLD."
    40 END

When you're asked:

    WHAT'S YOUR NAME AND AGE (NAME,AGE)?

Type in the answer such as:

    ASHER,14 [ENTER]

and the Screen will look like this:

    ASHER IS 14 YEARS OLD
    OK

(In this program, we used CLS to clear the prompt off the Screen before you answer the question.)

There are two things to notice about this short program:

  • String and numeric variable can be "mixed and matched" in the same prompt.
  • You can use a number of variable items with an INPUT statement.
Whether you have a single prompt or a dozen prompts, Micro Color BASIC will display the message ?EXTRA IGNORED if you enter more responses than you have prompts.

INPUT, used in the manner above, has a built-in PRINT statement. Although it's a little redundant, you can also use INPUT like this:

    10 CLS
    20 PRINT "WHAT IS YOUR NAME"
    30 INPUT NAME$
    40 CLS
    50 PRINT "HELLO"; NAME$
    60 END

This program does the same thing the first example in this chapter did (also listed below), but it takes two extra program lines to do it.

    10 CLS
    20 INPUT "WHAT IS YOUR NAME";NAME$
    30 PRINT "HELLO ";NAME$
    40 END

How about a really practical program that uses INPUT? Try this one:

    10 CLS
    20 REM *** CENTIGRADE TO FAHRENHEIT CONVERSION ***
    30 INPUT "WHAT IS THE TEMPERATURE IN DEGREES
       CENTIGRADE";C
    40 F = (9/5) * C + 32
    50 PRINT C; "DEGREES CELSIUS = "; F; "DEGREES
       FAHRENHEIT."
    60 END

There Has To Be An Easier Way...


INPUT can be entered using the Command Function Keys. Type:

    [CONTROL][@]

More Interaction...


Up to now, all the programs have been strictly one-shot affairs. You type RUN [ENTER], the Computer executes the program, prints the results on the TV (if any), and comes back with the OK prompt. To repeat the program, you have to type RUN [ENTER] again.

One thing about Computers is that they're especially good at any procedure which needs to be repeated. So how do you make the MC-10 repeat things? It's easy, just use...

The GOTO Command


Anytime Micro Color BASIC encounters the GOTO statement in a program line, it will immediately skip to ("goto") the specified line and continue program execution at that point. This is referred as an "unconditional branch" since Micro Color BASIC must branch to the specified line under any condition.

GOTO line number
line number
is a valid program line between 0 and 63999


If you tell Micro Color BASIC to goto a line number that is not in the program, an Unlisted Line Number Error message will be displayed.

Make one of the previous programs more efficient by using GOTO. Try this one:

    10 INPUT "WHAT IS YOUR NAME";NAME$
    20 PRINT "HELLO ";NAME$
    30 GOTO 10

When you RUN this program, you'll be asked your name and the MC-10 will print it on the TV over and over. This is called an "endless loop" since there is no way out of the program. It executes lines 10, 20, and 30-then loops back to 10 and repeats the process.

To get out of the endless loop, press [BREAK] (or turn the MC-10 off or press RESET).

Putting GOTO in the middle of a program will cause Micro Color BASIC to skip over all lines that are between the GOTO statement and the specified line. For instance:

    10 CLS
    20 PRINT "THIS IS LINE 20"
    30 GOTO 50
    40 PRINT "THIS IS LINE 40"
    50 PRINT "WE SKIPPED TO LINE 50"
    60 END

When you RUN this program, the TV will display:

    THIS IS LINE 20
    WE SKIPPED TO LINE 50
    OK

indicating that program line 40 was totally ignored.

Another way to create an endless loop is to GOTO the current line. This lets you "hold" whatever is currently being displayed (without returning to the OK prompt) because the program continues execution. For example:

    10 PRINT "PRESS [BREAK] TO RETURN TO 'OK'"
    20 GOTO 20

When you RUN this program, the Screen will display:

    RUN
    PRESS [BREAK] TO RETURN TO 'OK'

until you press [BREAK]. At that time, the message:

    BREAK IN 20
    OK

will appear.

There Has To Be An Easier Way...


GOTO can be entered using the Command Function keys. To enter this command, type:

    [CONTROL][J]

The ON...GOTO Command


Remember that we said GOTO was an "unconditional branching" statement. Micro Color BASIC allows you to vary the format of GOTO to allow it to test certain values before skipping to a line and to make multi-way branches. The ON...GOTO command does this.

ON test value GOTO line number
test value
is a numeric expression between 0-255.
line number is a valid program line number between 0 and 63999. Multiple line numbers must be separated by a comma.


The following program doesn't use ON...GOTO, but it should. RUN it anyway. Then we'll re-write the program so you can see some of the power of the ON...GOTO statement. (We're going to use a statement that hasn't been discussed yet-IF. It'll be described later.):

    10 INPUT "TYPE IN A NUMBER BETWEEN 1 AND 5";N
    20 IF N = 1 GOTO 110
    30 IF N = 2 GOTO 130
    40 IF N = 3 GOTO 150
    50 IF N = 4 GOTO 170
    60 IF N = 5 GOTO 190
    70 PRINT N; "IS NOT BETWEEN 1 AND 5"
    99 END
    110 PRINT "N = 1"
    120 END
    130 PRINT "N = 2"
    140 END
    150 PRINT "N = 3"
    160 END
    170 PRINT "N = 4"
    180 END
    190 PRINT "N = 5"
    199 END

(Hint: If you want to execute the program repeatedly and not have to type RUN [ENTER] over and over, replace each of the END statements with GOTO 10.)

To see how ON...GOTO can shorten your programs, delete lines 20, 30, 40, 50, and 60 (simply type the line numbers and press [ENTER]) and replace them with this single line:

    20 ON N GOTO 110, 130, 150, 170, 190

Now RUN the program. See, it does the same thing except it does it with four fewer program lines. What happens is this:

Line 20 says if N=1, then GOTO line 110; if N=2, then GOTO line 130; if N=3, then GOTO line 150;if N=4, then GOTO line 170;if N=5, then GOTO line 190. If N doesn't equal any of those numbers, then move on to the next program line (line 70).

There are variations on using ON...GOTO. For instance, change line 20 to:

    20 ON N - 1 GOTO 110, 130, 150, 170, 190

In this case, if you enter the number 3, Micro Color BASIC will subtract 1 from it and display the result as N=2, etc.

The GOSUB/RETURN Commands


A close relative of GOTO is the GOSUB statement which is used in conjunction with RETURN. When Micro Color BASIC is performing one routine, and it needs to execute a separate "subroutine" to complete the main routine, use GOSUB. Then, to return to the next statement of the main routine, use RETURN.

GOSUB line number
line number
is a valid program line number between 0 and 63999


RETURN


There are two important rules concerning the use of GOSUB:
  • Use END or GOTO statements directly ahead of the subroutine to insure that Micro Color BASIC can enter the subroutine only via a GOSUB.
  • Every subroutine must end with a RETURN.

So What is a Subroutine?


A subroutine is a short, but very specialized, program which you build into a large program to meet a specialized need. GOSUB is used to call that subroutine by directing program control to the specified line. As we said, RETURN returns control to the main program.

Type NEW [ENTER] and try this example:

    10 PRINT "1. THIS WILL BE PRINTED FIRST"
    20 GOSUB 100
    30 PRINT "3. THIS WILL BE PRINTED THIRD"
    40 END
    100 PRINT "2. THIS WILL BE PRINTED SECOND"
    110 RETURN

When you RUN the program, 1, 2 and 3 will be displayed in the correct order.

For a more powerful example of GOSUB, type NEW [ENTER] and this program (even though it has a couple of commands we haven't covered yet):

    10 INPUT "TYPE ANY NUMBER"; N
    20 GOSUB 1000
    30 ON T + 2 GOTO 50,70,90
    40 END
    50 PRINT "THE NUMBER IS NEGATIVE."
    60 END
    70 PRINT "THE NUMBER IS ZERO."
    80 END
    90 PRINT "THE NUMBER IS POSITIVE."
    100 END
    1000 IF N < 0 THEN T = -1
    1010 IF N = 0 THEN T = 0
    1020 IF N > 0 THEN T = +1
    1030 RETURN

There Has To Be An Easier Way...


Both GOSUB and RETURN can be entered using the Command Function keys.

To enter GOSUB, type:

    [CONTROL][D]

To enter RETURN, type:

    [CONTROL][F]

The ON...GOSUB/RETURN Command


Just like ON...GOTO, GOSUB can be used to branch subroutines. In fact, ON...GOSUB can be used exactly like ON...GOTO except that each branch to a subroutine must end with a RETURN statement like this:

    10 INPUT "WHAT'S BEHIND DOOR #1, #2, OR #3 (TYPE
        1,2,3)"; N
    20 ON N GOSUB 100,200,300
    30 END
    100 PRINT "YOU JUST WON AN MC-10 COMPUTER"
    110 RETURN
    200 PRINT "YOU JUST WON A CASSETTE RECORDER TO GO
        WITH YOUR MC-10"
    210 RETURN
    300 PRINT "YOU JUST WON A PRINTER TO USE WITH YOUR
        MC-10"
    310 RETURN

ON...GOSUB tells the Computer to look at the number following ON. In this case, that number is N. If it is 1, Micro Color BASIC goes to the subroutine beginning at the first line number following GOSUB. If N is 2, Micro Color BASIC goes to the subroutine beginning at the second line number; if N is 3, the Computer skips to the third line number. What if N is 4? Since this is not an option, Micro Color BASIC ignores it and goes to the next line in the program.






Comments should be directed to the following email address:
dsbrain@neosplice.com