Loading, please wait...

Relational Operators

Relational Operator is used to comparing two quantities and take a certain decision depending on their relation. If the specified relation is true it returns one. If the specified relation is false it returns zero.

 

Operator

Description

Example

==

Checks if the values of two operands are equal or not. If yes, then the condition becomes true.

(num1 == num2) is not true.

!=

Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true.

(num1 != num2) is true.

Checks if the value of left operand is greater than the value of the right operand. If yes, then the condition becomes true.

(num1 > num2) is not

true.

Checks if the value of left operand is less than the value of the right operand. If yes, then the condition becomes true.

(num1 < num2) is true.

>=

Checks if the value of left operand is greater than or equal to the value of the right operand. If yes, then the condition becomes true.

(num1 >= num2) is not

true.

<=

Checks if the value of left operand is less than or equal to the value of the right operand. If yes, then the condition becomes true.

(num1 <= num2) is true.

 

Example

 

/* C program for relational operators */

#include <stdio.h>
main()
{
int num1 = 25;
int num2 = 15;
int num3 ;
if( num1 == num2 )
{
printf("Line 1 – num1 is equal to num2\n" );
}
else
{
printf("Line 1 – num1 is not equal to num2\n" );
}
if ( num1 < num2 )
{
printf("Line 2 – num1 is less than num2\n" );
}
else
{
printf("Line 2 – num1 is not less than num2\n" );
}
if ( num1 > num2 )
{
printf("Line 3 – num1 is greater than num2\n" );
}
else
{
printf("Line 3 – num1 is not greater than num2\n" );
}
/* Try with the different values of num1 and num2 */
num1 =15;
num2 = 30;
if ( num1 <= num2 )
{
printf("Line 4 – num1 is either less than or equal to num2\n" );
}
if ( num2 >= num1 )
{
printf("Line 5 – num2 is either greater than or equal to num1\n" );
}
}

Output :

Line 1 – num1 is not equal to num2

Line 2 – num1 is not less than num2

Line 3 – num1 is greater than num2

Line 4 – num1 is either less than or equal to num2

Line 5 – num1 is either greater than or equal to num2