beautypg.com

B&B Electronics VFG3000 - Manual User Manual

Page 167

background image

C

ONFIGURING

P

ROGRAMS

P

ROGRAMMING

T

IPS

R

EVISION

1

P

AGE

151

T

HE

F

OR

L

OOP

You will notice that the

while

loop shown above has four elements…

1. The initialization of the loop control variable.

2. The evaluation of a test to see if the loop should continue.

3. The execution of the action to be performed by the loop.

4. The making of a change to the control variable.

The

for

loop allows elements 1, 2 and 4 to be combined within a single statement, such that

the action following the statement need only implement element 3. This syntax results in
something similar to the FOR-NEXT loop found in BASIC and other such languages.

The architecture of the

for

loop statement is as follow…

for ( initialization; condition; control ){

action1;

}

Using this statement, the example given above can be rewritten as…

int i, t;

for( i:=t:=0; i<10; i++ )

t += Data[i];

return t;

You will notice that the

for

statement contains three distinct elements, each separated by

semicolons. The first element is the initialization step, which is performed once when the loop
first begins; the next is the condition, which is tested at the start of each loop iteration to see if
the loop should continue; the final element is the induction step, which is used to make a
change to the control variable to move the loop on to its next iteration. Again, remember that
if you want more than one action to be included in the loop, include them in curly-brackets!

T

HE

D

O

L

OOP

This type of loop is similar to the

while

loop, except that the condition is tested at the end of

the loop. This means that the loop will always execute at least once.

The architecture of the

do

loop statement is as follow…

do {

action1;

} while ( condition );