The loop from lines 130 to 150 only ends when a key with an ASCII code
between 48 and 53 is pressed. These are the ASCII codes for the numbers 1 to 4
on .the keyboard, so this loop screens out accidental key depressions like Q or W.
Subtracting 48 from the ASCII code in line 160 gives a number from 1 to 4
again and line 170 uses this number to choose which of four procedures to
execute. If choice=1, the computer carries out PROCload, with choice_2, it
executes PROCsave, and so on. After carrying out the procedure the computer
continues with line 180.
The program avoids the problems that might arise with a wrong key depression
by only continuing when one of the keys 1 to 4 is pressed. However, the loop
from lines 130 to 150 can be omitted and the problem of incorrect keys handled
by an extension of the ON...PROC statement:
120 PRINT TAB(7,22)"Your choice, 1 to 4 ";
140 response=GET
160 choice=response-48
170 ON choice PROCLoad, PROCsave, PROCdraw, PROCmake_sure ELSE
PR0Cwrong_key
The computer executes PROCwrong_key if choice does not fall in the range 1
to 4. Only a single statement can follow the ELSE, although it need not be a
PROC, for example:
170 ON choice PROCload, PROCsave, PROCdraw, PROCmake_sure ELSE
PRINT"Wrong key ! "
ON...PROC is very useful, but note that it only works with numbers which
must range from one upwards in steps of one. Normally, therefore you will need
to carry out some kind of calculation in order to produce a suitable range of
values.
Error handling
You can reduce the time you spend correcting errors in your programs by using
procedures and sensible variable names, but it is inevitable that you will make
some mistakes. The computer is able to identify some types of error itself, and
gives an error message to let you know what is wrong.
You should always include an error-handling routine in your program. This
tells you (or anyone else using the program) as much about the error as
possible, and makes correcting it easier:
10 ON ERROR GOTO 50
20 MODE 130
30 PROCmain_program
C 45