EasyManua.ls Logo

Commodore 128 - Page 90

Commodore 128
448 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
program. The TRAP command advises you to locate and correct
an error, then resume program operation. Usually, the error-
trapping function is set in the first line of a program:
5 TRAP 100
tells the computer that if an error occurs to go to a certain line (in
this case, line 100). Line 100 appears at the end of the program,
and sets up a contingency. Neither line is executed UNLESS
there is an error. When an error occurs, the line with the TRAP
statement is enacted, and control is directed to another part of
the program. You can use these statements to catch anticipated
errors in entering data, resume execution, or return to text mode
from a graphics mode, to name just a few options. If you run the
last DO/LOOP example (which doubled numbers) without an
UNTIL statement, you can get an OVERFLOW error and the
program crashes. You can prevent that from happening by
adding two lines, one at the beginning of the program and one at
the end. For this example, you might add these two lines:
5 TRAP 100
100 IF N>1 THEN END
Even though N has been much greater than one for the entire
program, the statement is not considered until there is an error.
When the number overflows (is greater than the computer can
accept), the TRAP statement goes into effect. Since N is greater
than one, the program is directed to END (rather than crashing.)
Here is an example in which trapping is used to prevent a zero
from being input for division.
10 TRAP 1000
100 INPUT I CAN DIVIDE BY ANY NUMBER. GIVE ME A
NUMBER TO DIVIDE ;D
110 INPUT WHAT SHOULD I DIVIDE IT BY;B
120 A=D/B
130 PRINT D;“DIVIDED BY;B;“EQUALS;A
140 END
1000 IF B=0 THEN PRINTEVEN I CANT DO THAT
1100 INPUT PICK A DIFFERENT NUMBER;B:
RESUME 120
Notice the RESUME in line 1100. This tells the computer to return
to the line mentioned (in this case, 120) and continue. Depending
5-12

Table of Contents

Other manuals for Commodore 128

Related product manuals