EasyManua.ls Logo

Commodore VIC-20 - Page 107

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...
Chapter 3 Programming the
VIC
20
Computer 93
Statements between
FOR
and
NEXT
reexecute the number
of
times
specified by the index value directly after
FOR.
In
the illustration above this
index variable
is
I. I increases in value from 0
to
99
in increments
of
1.
The
first time the assignment statement
is
executed, I will equal 0 and the
assignment statement on line
30
will be executed as follows: .
30
A(0)-e
I increases by the step,
or
increment, size, which
is
specified on line
20
as
1.
I
therefore equals 1 the second time the assignment statement
on
line
30
is
executed. The assignment statement has effectively become
I continues to increment by the specified step value until the maximum value
of
99
is
reached
or
exceeded.
The step value does
not
have
to
be
1;
it can have any value. Change the
step value
to
5 on line
20
and reexecute the program. Now the assignment
statement
is
executed only
20
times, since incrementing I by 5 nineteen times
will take it to
95
(the 20th increment will take it to 100, which
is
more
than
the maximum value
of
99).
The step size does
not
have
to
be positive. But if the step size
is
negative,
the initial value
of
I must be larger
than
the final value
of
I.
For
example, if
the step size
is
-1
and
we
want to initialize
100
elements
of
A(I) with values
ranging from 0 to 99, then
we
would have
to
rewrite the statement on line
20
as follows:
10
DIM
A(99)
20
FOR
1-99
TO
0
STEP
-1
30
A(I)-I
35
PRINT
fl(
l)
i
40
NEXT
I
80
GOTO
80
Execute this program
to
test the negative step.
In
this example the initial and final values for I, and the step size, are
treated as integers. You must, however, represent these three values using
floating
point
variables
or
expressions. Expressions will be evaluated
to
a
floating point result. The floating point result will be converted
to
an
integer
using the round-off rules described earlier in this chapter.
Because round-off rules can cause problems, you are strongly urged to
use beginning values, ending values, and step sizes as integers.
Do
not
use

Other manuals for Commodore VIC-20

Related product manuals