Break and continue, Break and continue -27 – Rockwell Automation 2098-IPD-xxx Ultra5000 C Programming using the Motion Library User Manual

Page 39

Advertising
background image

Publication 2098-PM001E-EN-P — July 2002

Programming Motion Control in C

1-27

the update (expression3) is evaluated. The loop is repeated until test
(expression2) is false or zero.

for (expression1;expression2;expression3)

statement;

/* if expression2 is non-zero, loop until zero

*/

The initialize and update components (expression1 and expression3)
may be omitted, but the semicolons must remain. If the test
component (expression2) is omitted, it is considered to be true and an
infinite loop occurs, which must be exited by a break or return
statement.

The while and for statements are entry-condition loops; the decision
to perform the loop is made before the loop is traversed. The
do-while statement is an exit-condition loop; the decision to perform
is made after the loop is traversed.

The do while statement creates a loop that repeats until the test
expression becomes false or zero.

do

statement

while (expression);

The statement is performed, and then the expression is evaluated. The
statement portion is repeated until the expression becomes false or
zero. Because the do while statement is an exit-condition loop, the
loop must be performed at least once. Programming convention is to
place braces around the statement, even though they are unnecessary,
to prevent a person from accidentally reading the while portion as the
beginning of a while loop.

Break and Continue

As discussed in the switch section, a break statement provides a
method to exit from for, while, do and switch statements. The
continue statement causes the program to pass over the remaining
statements in a loop. It can be used with the for, while, and do
statements, but not with a switch statement. A continue statement
causes the next loop to start on the while and for statements. In a do
while loop, the exit condition is tested and then, if necessary, the next
loop is started.

Advertising