EasyManua.ls Logo

Commodore VIC-20 - Subroutine Statements

Commodore VIC-20
404 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...
96 The VIC 20
User
Guide
Loop structures are easy to visualize and use. There
is
only one com-
mon error to watch out for:
Do
not terminate
an
outer loop before you
terminate
an
inner loop.
For
example, the following loop structure
is
illegal:
.
X"'25
TO
347
STEP
3
XT
I
If
you do not include the index variable in the NEXT statement,
program logic will automatically terminate loops correctly, since there
is
only one possible correct loop termination each time a NEXT statement
is
encountered.
Every program must have the same number of
FOR
and NEXT state-
ments, since every loop must begin with a
FOR
statement and end with a
NEXT statement.
For
example, suppose there are two
FOR
statements, but
only one NEXT statement. The second
FOR
statement constitutes an inner
loop
that
will execute correctly. But the outer loop has no NEXT statement
to terminate it, so the program
will
execute incorrectly. Too many NEXT
statements
will
also cause a syntax error.
Subroutine
Statements
Once you start writing programs that are more than a
few
statements
long, you will quickly find short routines that are used repeatedly.
For
example, suppose you have
an
array variable (such as A(I))
that
is
reinitial-
ized frequently
at
different points in your program. Would you simply
repeat the three instructions that constitute the FOR-NEXT loop described
earlier? Since there are only three instructions, you may as
well
do so.
Suppose you have to initialize the array and then execute ten or eleven
instructions that process array
data
in some fashion.
If
you had to use this
loop many times within one program, rewriting ten to fifteen statements
each time you wished to use the loop would take time, but more importantly,
it would waste a good deal of computer memory. This may be illustrated as
follows:

Other manuals for Commodore VIC-20

Related product manuals