beautypg.com

B&B Electronics VFG3000 - Manual User Manual

Page 166

background image

P

ROGRAMMING

T

IPS

V

LINX

F

IELDBUS

G

ATEWAY

M

ANAGER

U

SER

M

ANUAL

P

AGE

150

the

do

loop tests the condition afterwards. The

for

loop is a quicker way of defining a

while

loop, allowing you to combine three common elements into one statement.

You should note that some care is required when using loops within your programs, as you
may make a programming error which results in a loop that never terminates. Depending on
the situation in which the program is invoked, this may seriously disrupt the terminal’s user
interface activity, or its communications. Loops which iterate too many times may also cause
performance issues for the subsystem that invokes them.

T

HE

W

HILE

L

OOP

This type of loop repeats the action that follows it while the condition in the

while

statement

remains true. If the condition is never true, the action will never be executed, and the loop
will perform no operation beyond evaluating the controlling condition. If you want more than
one action to be included in the loop, be sure to surround the multiple statements in curly-
brackets, as with the

if

statement. The example below initializes a pair of local variables, and

then uses the first to loop through the contents of an array, totaling the first ten elements, and
returning the total value to the caller…

The architecture of the

while

loop statement is as follow…

while ( condition ){

Action;

}

int i:=0, t:=0;

while( i < 10 ) {

t := t + Data[i];

i := i + 1;

}

return t;

The example below shows the same program, but rewritten in a compressed form. Since the
loop statement now controls only a single action, the curly-brackets have been omitted…

int i:=0, t:=0;

while( i < 10 )

t += Data[i++];

return t;