Relational Operators




Relational Operators: Relational operators determine the relation among different operands. There are six different relational operators for comparing number and characters. But they don't work with strings.

If the comparison is true, the relational expression result into the value 1 and if the comparison if false, the relational expression result into the value 0.

Relational operators are:
1. <(less than)
2. >(greater than)
3. <=(less than or equal to)
4. >=(greater than or equal to)
5. ==(equal to)
6. !=(not equal to)

Program to illustrate relational operators:

#include<iostream.h>
void main()
{
    int a=10;
    int b=20;

   if(a>b) // check if a is greater than b
  {
   cout<<"True";
  }  
   else
  {
   cout<<"False";
  }

  
  if(a<b)   // check if a is smaller than b
  {
   cout<<"True";
  }
  else
  {
   cout<<"False";
  }


  if(a>=b) // check if a is greater and equal to b
  {
   cout<<"True";
  }
 else
 {
   cout<<"False";
 }

 
  if(a<=b)  // check if a is smaller and equal to b
  {
   cout<<"True";
  }
 else
 {
   cout<<"False";
 }


if(a==b)  // check if a is equal to b
  {
   cout<<"True";
  }
 else
 {
   cout<<"False";
 }
 

if(a!=b)   // check if a is not equal to b
  {
   cout<<"True";
  }
 else
 {
   cout<<"False";
 }
   
} // main function ends

The output of the above program simply depend upon if-condition, If the condition inside parenthesis () is true, simply statement between opening and closing braces of if i.e {} will be executed and if the condition of if-condition inside parenthesis() is false then else statement inside braces{} will be executed.

Output :
False
True
False
True
False
True

Post a comment

0 Comments