Statements: gosub [ln num] – return – Rockwell Automation 1771-DB Basic Module User Manual - Series A User Manual
Page 60
Operating Functions
Chapter 5
5–23
Since B=0, C=10 and D=2,
the PRINT statement at line 20 executes
6 times. The values of “A” printed are 0, 2, 4, 6, 8 and 10. “A” represents
the name of
the index or loop counter. The value of “B” is the starting
value of the index. The value
of “C” is the limit value of the index and
the
value of “D” is the increment to the index. If the STEP statement
and the value “D” are omitted,
the increment value defaults to 1,
therefore, STEP is an optional statement.
The NEXT statement adds
the value of “D” to the index.
The index is then compared to the value of
“C”, the limit.
If the index is less than or equal to the limit, control
transfers
back to the statement after the FOR statement. Stepping
“backwards”
(FOR I= 100 TO 1 STEP–1) is permitted in the BASIC
Module. You may not
omit the index from the NEXT statement in the
module (The NEXT statement
is always followed by the appropriate
variable). You may nest FOR-NEXT loops up to 9 times.
Examples:
u10 FOR I=1 TO 4
u10 FOR I=O TO 8 STEP 2
u20 PRINT I,
u20 PRINT I
u30 NEXT I
u30 NEXT I
u40 END
u40 END
uRUN
uRUN
1 2 3 4
0
2
4
6
8
READY
u
READY
u
Mode: RUN
Type: CONTROL
GOSUB
The GO SUB [ln
num] statement causes the BASIC Module to
transfer
control of the program directly to the line number ([ln num])
following the GOSUB statement.
In addition, the GOSUB statement
saves the location
of the statement following GOSUB on the control
stack so that you can perform a RETURN statement to return
control to
the statement
following the most recently executed GO SUB
STATEMENT.
5.4.11
Statements: FOR – TO –
(STEP) – NEXT (continued)
5.4.12
Statements: GOSUB
[ln num] – RETURN