Relational Operators in C Languages

Relational Operators : Relational operators are used in Boolean conditions or expressions, that return either true or false.

The relational operators returns zero values or non-zero values. The zero values is taken as false, while the non-zero value is taken as true.

A simple relational expression contains only one relational operator and takes the following form : ,/p>

ae - 1 relational operator ae - 2

ae - 1 and ae - 2 are arithmetic expressions, which may be simple constants, variables or combination of them and arithmetic operators.
C supports six Relational operators and are as follows :

Operator Meaning
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== is equal to
!= is not equal to

Relational operators are used to compare two quantities.,/p>

Relational operators are binary operators, because they act on two operands.

The operators <, <=, >, >= will be given the highest priority over ==, !=

when equal priority operators are there in an expression, the operators will be evaluated from left to right i.e they follow left to right associativity.

Example's :
4.5 <= 10            true
4.5 < -10            false
-35 >= 0            false
10 < 7 + 5           true
(10+5) == (3*5)           true

a + b == c + d true, only if the sum of values of a and b is equal to the sum of values of c and d.

When arithmetic expressions are used on either side of a relational operator, then the arithmetic expressions will be evaluated first and then the results will be compared. That is, arithmetic operators have a higher priority over relational operators.

Relational expressions are used in decision statements such as if and while to decide the course of action of a running program.

Relational Operator Complements :

Among the six relational operators, each one is a complement of another operator.

> is complement of <=
< is complement of >=
== is complement of !=

We can simplify an expression involving the not and the relational operators using the complements as shown below :

Actual one Simplified one
!(x + y) x >= y
!(x > y) x <= y
!(x != y) x == y
!(x <= y) x > y
!(x >= y) x < y
!(x == y) x != y


Post a Comment