Previous | Home | Next |

### Relational operators

Often it is required to compare the relationship between operands and bring out a decision and program accordingly. This is when the relational operator come into picture. C supports the following relational operators:

Operator | Meaning |

< | is less than |

<= | is less than or equal to |

> | is greater than |

>= | is greater than or equal to |

== | is equal to |

!= | is not equal to |

It is required to compare the marks of 2 students, salary of 2 persons, we can compare them using relational operators. A simple relational expression contains only one relational operator and takes the following form.

*exp1 relational operator exp2 :* Where exp1 and exp2 are expressions, which may be simple constants, variables or combination of them. Given below is a list of examples of relational expressions and evaluated values.

6.5 <= 25 TRUE -65 > 0 FALSE 10 < 7 + 5 TRUE

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

C has the following logical operators, they compare or evaluate logical and relational expressions.

Operator | Meaning |

&& | Logical AND |

|| | Logical OR |

! | Logical NOT |

### Logical AND (&&)

This operator is used to evaluate 2 conditions or expressions with relational operators simultaneously. If both the expressions to the left and to the right of the logical operator is true then the whole compound expression is true.

### Example

a>b && x==10

The expression to the left is a > b and that on the right is x == 10 the whole expression is true only if both expressions are true i.e., if a is greater than b and x is equal to 10.

### Logical OR (||)

The logical OR is used to combine 2 expressions or the condition evaluates to true if any one of the 2 expressions is true.

### Example

a<m || a<n

The expression evaluates to true if any one of them is true or if both of them are true. It evaluates to true if a is less than either m or n and when a is less than both m and n.

### Logical NOT (!)

The logical not operator takes single expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it just reverses the value of the expression.

### Example

! (x >= y)

C allows usage of three logical operators, namely, &&, || and !.These are to be read as 'AND' 'OR' and 'NOT' respectively. here we can take an example to under stand the logical operators which is given below:

main( ) { int m1, m2, m3, m4, m5, per ; printf ( "Enter marks in five subjects " ) ; scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ; per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ; if ( per >= 60 ) printf ( "First division" ) ; if ( ( per >= 50 ) && ( per < 60 ) ) printf ( "Second division" ) ; if ( ( per >= 40 ) && ( per < 50 ) ) printf ( "Third division" ) ; if ( per < 40 ) printf ( "Fail" ) ; } /* As can be seen from the second if statement, the && operator is used to combine two conditions. 'Second division' gets printed if both the conditions evaluate to true. If one of the conditions evaluate to false then the whole thing is treated as false. By the above example we can understand the else if condition which is only the arrangement of else with if. */

### Example

/* In this program sex and ms are the variable of char type and age is the variable of integer type and operator is denoted as && and or operator is denoted as || */ #include <stdio.h> #include <conio.h> main() { char sex, ms; int age; clrscr(); printf ("Enter Age, Sex, Merital Status"); scanf ("%d %c %c" &age,&sex,&ms); if ((ms=='M')||(ms=='U'&&sex=='M'&&age>30)||(ms=='M')|| (ms=='M'&&sex=='F'&&age>25)) printf ("Driver is insured"); else printf ("Driver is not insured"); }

*Output :*Enter Age, Sex, Marital Status

35 F M

Driver is insured

Previous | Home | Next |