5 program looping – Rockwell Automation 57C610 Enhanced Basic Language, AutoMax User Manual

Page 53

Advertising
background image

6Ć13

(SWITCH_34% is not a boolean variable or a valid

Ărelational expression)

150 IF A% > B% THEN GOTO 700

ELSE GOTO 400

END_IF

(The keyword THEN must be the last item on the first line)

Note that Version 2.0 and later of the AutoMax Programming

Executive supports both the IFĆTHENĆELSE format described above

and the IFĆTHEN format used in Version 1.0 of the AutoMax

Programming Executive (M/N 57C304Ć57C307). Version 1.0,

however, supports only the IFĆTHEN format.

6.5

Program Looping

A loop is the repeated execution of a set of statements. Placing a

loop in a program saves you from duplicating routines and

enlarging a program unnecessarily.
For example, the following two programs will print the numbers from

1 to 10:

Program Without Loop

Program With Loop

10 PRINT 1

10 I% =1

20 PRINT 2

20 PRINT I%

30 PRINT 3

30 I% = I% +1

40 PRINT 4

40 IF I% < = 10 THEN 20

50 PRINT 5

50 END

60 PRINT 6

70 PRINT 7

80 PRINT 8

90 PRINT 9

100 PRINT 10

110 END

Both of these programs would result in the following being printed:
1

2

3

4

5

6

7

8

9

10
The program with a loop first initializes a control variable, I%, in line

10. It then executes the body of the loop, line 20. Finally, it

increments the control variable in line 30 and compares it to a final

value in line 40.
Without some sort of terminating condition, a program can run

through a loop indefinitely. The FOR and NEXT statements set up a

loop wherein BASIC tests for a condition automatically each time it

runs through the loop. You decide how many times you want the

loop to run and you set the terminating condition.
The FOR statement has the following format:

FOR variable = expression_1 TO expression_2 {STEP

expression_3}

Advertising
This manual is related to the following products: