CodesandTutorials
Next generation website theme is here (black, neon and white color) - Reduces eye strain and headache problems.
You are here - Home > Programming > C > Basics

Relational Operators


Description

The relational operators allows us to compare the values of one to another, where both the values should be of same type i.e. arithmetic or string. Following are the relational operators used to check the values, whether they are equal, not equal, smaller, greater etc.

== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to

Example showing the use of relational operators

Source code


  • // using relational operators
  • #include <stdio.h>
  • #include <conio.h>
  • void main()
  • {
  • int a,b;
  • clrscr();
  • printf("Enter the value of A : ");
  • scanf("%d",&a);
  • printf("Enter the value of B : ");
  • scanf("%d",&b);
  • if(a>b)
  • {
  • printf("\nA is greater than B");
  • }
  • if(a<b)
  • {
  • printf("\nB is greater than A");
  • }
  • if(a==b)
  • {
  • printf("\nA and B are equal");
  • }
  • if(a!=b)
  • {
  • printf("\nA is not equal to B");
  • }
  • getch();
  • }

Program working steps

  • First 5 lines of the program are basic about comment, preprocessor directive and void main function.
  • Similar to previous program, it takes two input from the user and stores into the respective variables.
  • Line no.13 if(a>b) statement, If this statement statisfies the condition then it will print loop body of if statement i.e. 'A is greater than B.'.
  • Similar to this, other line no.17,21,24 will be checked, whether the condition of the statemet is true or false. If true, it will print the block statement else it will jump to the next statement.
  • For eg: If user enter 5 and 4 value of a and b variable, then line no.13 and 25 statements will be true printing their body statements.

Tip Box

Operator precedence

Priority Operator
1st <
2nd <=
3rd >
4th >=
5th ==
6th !=

Advertisement





Terms of Use | Privacy Policy | Contact Us | Advertise
CodesandTutorials © 2014 All Rights Reserved