Relational Operator


Relational Operator

A relational operator is used to check the relationship between two operands. For example,

  1. Check if a>b,
  2. Check if a=b,
  3. Check if a

Relational operators return “true” or “false” according to the data, if in example 1 if a is greater than b then it returns “True” for that particular expression. There are 6 relational operators:

Operator Symbol Relational Operation
== Equal to
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Expression’s first value = a

Expression’s second value = b for upcoming examples:

  1. ==

  2. Return “true” if the value of "first value" is equal to the value of "second value" else return “false” for the expression.

  3. !=

  4. Return “true” if the value of "first value" isn’t equal to the value of "second value" else return “false” for the expression.

  5. >

  6. Return “true” if the value of "first value" is greater than the value of "second value" else return “false” for the expression.

  7. <

  8. Return “true” if the value of "first value" is smaller than the value of "second value" else return “false” for the expression.

  9. >=

  10. Return “true” if the value of "first value" is greater than the equals to the value of "second value" else return “false” for the expression.

  11. <=

  12. Return “true” if the value of "first value" is smaller than equals to the value of "second value" else return “false” for the expression.

Relational operators:

/* A program to show various relational operation */ #include<stdio.h> #include<conio.h> void main() { int a= 15; int b= 10; int c; if(a == b){ printf("line 1- a is equal to b \n"); }else { printf("line 1- a is not equal to b \n"); } if(a < b){ printf("line 2- a is less than b \n"); } else { printf("line 2- a is not less than b \n"); } if(a > b){ printf("line 3- a is greater than b \n"); } else { printf("line 3- a is not greater than b \n"); } if(b>=a){ printf("line 4- b is either greater than or equal to b\n"); } if(a<=b){ printf("line 5- a is either less than or equal to b\n"); } getch(); }

output:

line 1- a is not equal to b line 2- a is not less than b line 3- a is greater than b line 4- b is either greater than or equal to b line 5- a is either less than or equal to b