For-to-(step)-next – Rockwell Automation 1771-DB BASIC MODULE User Manual

Page 163

Advertising
background image

Chapter
Statements

11

11 -11

Use the FOR-TO-(STEP)-NEXT statement to set up and control program
loops.

If the STEP statement and the value are omitted, the increment value
defaults to 1, therefore; STEP is an optional statement. The NEXT
statement returns the loop to the beginning of the loop and adds the value
of the STEP expr to the current index value. The current index value is
then compared to the limit value of the index.

If the index is less than or equal to the limit, control transfers back to the
statement after the FOR statement. Stepping backward (FOR I = 100 TO 1
STEP –1) is permitted in the BASIC module. The NEXT statement is
always followed by the appropriate variable.

The control stack (C-stack) stores all information associated with loop
control. The C-stack is 157 bytes long. FOR-NEXT loops use 17 bytes of
the C-stack. Do not improperly exit this loop or a C-stack error occurs.
See CLEARS (page 11 -3).

Important: Excessive nesting exceeds the limits of the C-stack, generating
an error, and causing the module to enter Command mode.
For more information see control stack, page 8 -1.

Syntax

FOR

expr TO expr STEP expr

.

.

NEXT

expr

Example

>5 E=0 : C=10 : D=2

>10 FOR A=E TO C STEP D

>20 PRINT A

>30 NEXT A

>40 END

>RUN

0

2

4

6

8

10

Since E=0, C=10, D=2, and the PRINT statement at line 20 executes 6
times, the values of A that are printed are 0, 2, 4, 6, 8 and 10. A represents
the name of the index or loop counter. The value of E 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.

FOR-TO-(STEP)-NEXT

Advertising