Rockwell Automation 2098-IPD-xxx Ultra5000 C Programming using the Motion Library User Manual
Page 37

Publication 2098-PM001E-EN-P — July 2002
Programming Motion Control in C
1-25
The last statement (else) performs the default or “none of the above”
case, and may be omitted in obvious situations. The structure is
shown below.
if (expression1)
/* If expression1 is 1,
perform statement1 */
statement1;
else if (expression2)
/* If expression2 is 1,
perform statement2 */
statement2;
else if (expression3)
/* If expression3 is 1,
perform block3-4 */
{
statement3;
statement4;
}
else
/* default to statement5
*/
statement5;
Else-if is what is known as a nested statement. A non-typical indented
alternative for writing the statement shows the functionality of the
statement nesting.
if (expression1)
/* initial if-else
statement */
statement1;
else
if (expression2)
/* nested if-else
statement */
statement2;
else
if (expression3)
/* another nested if-else
statement */
{
statement3;
statement4;
}
else
statement 5;
The nested else-if program consists of a series of if-else statements for
which the statement part of the else is another if-else statement. The
rule to remember is that the else goes with the preceding if, unless
braces indicate otherwise.