Jan 23, 2012

Relational & Logical Operators


Relational operators are used to compare two values. There are six (6) operators as listed in the following table.

Operator Meaning
==
Equal to
!=
Not equal to
<
Less than
<=
Less than or equal to
>
Greater than
>=
Greater than or equal to

Relational operators have lower precedence than arithmetic operators. For example, consider the following expression.

x + y > z

Since arithmetic operators precede relational operators. The expression is evaluated as

(x + y) > z

Assumes that x, y and z is 2, 3 and 4. Then, the expression is said TRUE. An expression is TRUE if it satisfies the condition and FALSE otherwise. In C, TRUE has the value of 1 while FALSE has the value of 0.

There are three (3) logical operators which are AND (&&), OR (||) and NOT (!). More than one expression can be combined using AND (&&) and OR (||) operators. For example, consider the following expression.

a > b && x == y

If both a > b and x == y are TRUE, the expression returns,

1 && 1

which will returns 1 (TRUE).

! is used to invert the logical value of an expression. For example,

!(x > 2)

Assume that x is 3 then the expression is said to be FALSE.