Campbell Scientific CR9000X Measurement and Control System User Manual

Page 339

Advertising
background image

Section 9. Program Control Instructions

Changing the value of counter while inside a loop can make the
program more difficult to read and debug.

TIP

You can nest For...Next loops by placing one For...Next loop within another.
Give each loop a unique variable name as its counter. The following
construction is correct:

For J = 5 To 1 Step -1

'Loop 5 times backwards.

For I = 1 To 12

'Loop 12 times.

. . . .

'Run some code.

Next I

. . .

'Run some code.

Next J

. . . .

'Run some code.

If you omit the variable in a Next statement, the value of Step
increment is added to the variable associated with the most
recent For statement. If a Next statement is encountered before
its corresponding For statement, an error occurs.

NOTE

Nested For...Next Statement Bubble Sort Example

If

Flag(3)

Then

'Perform Bubble Sort based on

Flag(3)

For

K = 1

To

29

For

I = 30

To

(K) Step -1

If

PlaceDist(I) > PlaceDist(K)

Then

DistD = PlaceDist(K)

'Dummies to hold Place K values

TractorD = TractorNum(K)

PlaceDist(K) = PlaceDist(I)

'Assign New Standing

TractorNum(K) = TractorNum(I)

PlaceDist(I) = DistD

TractorNum(I) = TractorD

EndIf

Next

I

Next

K

Flag(3) = False

EndIf

This next example fills odd elements of X up to 40 * Y with odd numbers.

For

I = 1

To

40 * Y

Step

2

X( I ) = I

Next

I

9-9

Advertising