Relational and logical operators, Conditional expressions – Rockwell Automation 2098-IPD-xxx Ultra5000 C Programming using the Motion Library User Manual

Page 33

Advertising
background image

Publication 2098-PM001E-EN-P — July 2002

Programming Motion Control in C

1-21

Relational and Logical
Operators

The relational operators (<, <=, >=, >) make comparisons that test
expressions. They have lower precedence than arithmetic operators,
but higher precedence than the equality operators (==, !=). The logical
operators (&& and ||) have a lower precedence than either the
relational operators and equality operators.

Each of these operators (except the unary !) compares the value to the
left of the operator to the value at its right.

The table below lists the precedence and describes the functions of
these operators.

The numeric value of relational and logical expressions is 1 if the
relation is true, and 0 if the relation is false. The unary negation
operator ! converts a non-zero operand into 0 and a zero operand into
1.

Conditional Expressions

Conditional expressions are a compact way of writing if-else
statements using “?”, the ternary operator. (If-Else is introduced on
page 1-24.)

The operator requires three operands, each of which is an expression,
and the condition is evaluated from left to right.

expressionl ? expression2 : expression3

The value of the whole expression equals the value of expression2 if
expression1 is true, else it equals the value of expression3.

Operator:

Precedence:

Function:

Limitations:

<

1

Is less than

<=

Is less than or equal to

Rounding may cause
errors with floating
point numbers.
(e.g., 3 * 1/3 is 1, but 3
* 0.333333 is
0.999999).

>=

Is greater than or equal to

>

Is greater than

==

2

Is equal to

!=

Is not equal to

&&

3

And

||

4

Or

!
(unary form)

5

Not

Advertising