If-else, Else-if, If-else -24 else-if -24 – Rockwell Automation 2098-IPD-xxx Ultra5000 C Programming using the Motion Library User Manual

Page 36

Advertising
background image

Publication 2098-PM001E-EN-P — July 2002

1-24

Programming Motion Control in C

In grouping example A, the assignment statement is included in the
while loop, because a while loop runs from the while to the next
semicolon.

In grouping example B, the braces group both the assignment and
MoveAbsolute statements into a single block. The entire compound
statement is executed as a single loop. Thus the MoveAbsolute
function is called each pass through the while loop.

If-Else

The if-else statement is a branching statement. It allows a C program
to execute one of two possible paths.

The general form of the if-else statement is:

if (expression)

statement1;

else

statement2;

The if statement tests the parenthesized condition. If expression is true
(nonzero), statement1 is performed. If only the if statement is used,
the command is ignored when the expression is false.

The else statement provides a second or default course of action. If
expression is false or zero, statement2 is performed.

The statements may be simple or compound, and by convention are
indented. More than one statement may be grouped under the if or
else by using braces to create a single block.

if (expression)

{

statement1;

statement 2;

/* braces create a compound statement block */

}

else

statement3;

Else-If

The else-if statement extends the if-else structure to provide multiple
choices. The statement is constructed as a series of if statements. The
expressions are evaluated in order; any true expression is executed
and the series then terminates.

Advertising