Comparision Operators Examples in C++


In C++, comparison operators are used to compare two values or expressions and return a Boolean value (true or false) based on whether the comparison is true or false. C++ provides the following comparison operators:

  • Equal to (==): Returns true if the values on both sides of the operator are equal, otherwise returns false.
  • Not equal to (!=): Returns true if the values on both sides of the operator are not equal, otherwise returns false.
  • Greater than (>): Returns true if the value on the left side of the operator is greater than the value on the right side, otherwise returns false.
  • Less than (<): Returns true if the value on the left side of the operator is less than the value on the right side, otherwise returns false.
  • Greater than or equal to (>=): Returns true if the value on the left side of the operator is greater than or equal to the value on the right side, otherwise returns false.
  • Less than or equal to (<=): Returns true if the value on the left side of the operator is less than or equal to the value on the right side, otherwise returns false.

Source Code Example : 1

#include<iostream>
using namespace std;
int main()
{
    int x=10,y=10;
    if(x==y)
    {
        cout<<"\nEqual to : "<<x;
    }
    return 0;
}
To download raw file Click Here

Output

Equal to : 10

Source Code Example : 2

#include<iostream>
using namespace std;
int main()
{
    int x=10,y=5;
    if(x>y)
    {
        cout<<"\nGreatest value : "<<x;
    }
    return 0;
}
To download raw file Click Here

Output

Greatest Value: 10

Source Code Example : 3

#include<iostream>
using namespace std;
int main()
{
    int x=10,y=5;
    if(x>=y)
    {
        cout<<"\nGreater than or equal to : "<<x;
    }
    return 0;
}
To download raw file Click Here

Output

Greater than or equal to : 10

Source Code Example : 4

#include<iostream>
using namespace std;
int main()
{
    int x=10,y=15;
    if(x<y)
    {
        cout<<"\nSmallest value : "<<x;
    }
    return 0;
}
To download raw file Click Here

Output

Smallest value : 10

Source Code Example : 5

#include<iostream>
using namespace std;
int main()
{
    int x=10,y=15;
    if(x<=y)
    {
        cout<<"\nSmaller than or equal to : "<<x;
    }
    return 0;
}
To download raw file Click Here

Output

Smaller than or equal to :10

Source Code Example : 6

#include<iostream>
using namespace std;
int main()
{
    int x=10,y=5;
    if(x!=y)
    {
        cout<<"\nNot Equal to : "<<x;
    }
    return 0;
}
To download raw file Click Here

Output

Not equal to: 10

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples