Commodore VIC-20 User Manual page 108

Hide thumbs Also See for VIC-20:
Table of Contents

Advertisement

94
The VIC 20 User Guide
expressions, since this unnecessarily complicates the program.
If
you must
calculate one of these values, it is simpler and faster to do so in a separate
statement.
If
the step size is 1 (which is frequently the case), you do not have to
include a step size definition. In the absence of any definition, VIC BASIC
assumes a step size of 1. Therefore, the statement on line 20 could be
rewritten as follows:
10
DIM
A(99)
15 REM USE
A
STEP SIZE OF 1
20
FOR
1-0
TO
99
30
A(I)-I
35
PRINT
A(l);
40
NEXT
I
S0
OOTO
S0
Also, you do not need to specify the index variable in the NEXT
statement. But if you do, it will make your program easier to read.
NESTED LOOPS
The FOR-NEXT structure is referred to as a program loop, since
statement execution loops from FOR to NEXT and back to FOR. This loop
structure is very common; almost every BASIC program that you write will
include one or more such loops. Loops are so common that they are
frequently nested. The statement sequence occurring between FOR and
NEXT can be of any length; it can run to tens or even hundreds of state-
ments. And within these tens or hundreds of statements, additional loops
may occur. The following illustration shows a single level of nesting:
10
DIM
A(99)
20
FO~
1-0 TO 99
30
A(l)-I
40
REM DISPLAY ALL VALUES OF A(I) ASSIGNED THUS FAR
50
FOR
J
a
0
TO
I
60 PRINT A(J)
70 NEXT J
S0 NEXT I
90 GOTO 99
Complex loop structures appear frequently, even in relatively short
programs. Hereis an example showing the FOR and NEXT statements, but
none of the intermediate statements.

Advertisement

Table of Contents
loading

Table of Contents