Open In App

Comparison Operators in Programming

Last Updated : 21 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Comparison Operators in programming are used to compare values and determine their relationship, such as equality, inequality, greater than, less than, etc. They evaluate expressions and return a Boolean value (true or false) based on the comparison result, crucial for decision-making in conditional statements and loops.

What is a Comparison Operator?

Comparison Operator is an operator that compares two operands and return true if the comparison evaluates to true otherwise, it returns false. They are important in programming, as they help us to compare values and expressions. The return value of a comparison is either true or false. 

Common Comparison Operators:

  • Equality Operator (==).
  • Inequality Operator (!=)
  • Greater Than Operator (>)
  • Less Than Operator (<)
  • Greater Than or Equal To Operator (>=)
  • Less Than or Equal To Operator (<=)

Equal to (==) Comparison Operator:

The “equal to” (==) operator is a comparison operator widely used in programming languages to determine whether two values or expressions are equal.

Example of Equal to (==) Comparison Operator:

Here are the example of Equal to (==) Operator in different language:

C++
#include <iostream>
using namespace std;
int main()
{
    int x = 5;
    int y = 10;
    // x == y returns true if x is equal to y else it
    // returns false
    if (x == y) {
        cout << "x is equal to y" << endl;
    }
    else {
        cout << "x is not equal to y" << endl;
    }
    return 0;
}
C
#include <stdio.h>
int main()
{
    int x = 5;
    int y = 10;
    // x == y returns true if x is equal to y else it
    // returns false
    if (x == y) {
        printf("x is equal to y\n");
    }
    else {
        printf("x is not equal to y\n");
    }
    return 0;
}
Java
public class Main {
    public static void main(String[] args)
    {
        int x = 5;
        int y = 10;
        // x == y returns true if x is equal to y else it
        // returns false
        if (x == y) {
            System.out.println("x is equal to y");
        }
        else {
            System.out.println("x is not equal to y");
        }
    }
}
Javascript
let x = 5;
let y = 10;
// x == y returns true if x is equal to y 
// else it returns false
if (x == y) {
    console.log("x is equal to y");
} else {
    console.log("x is not equal to y");
}
Python3
x = 5
y = 10
# x == y returns true if x is equal to y else it returns false
if x == y:
    print("x is equal to y")
else:
    print("x is not equal to y")

Output
x is not equal to y

Not equal to (!=) Comparison Operator:

The “not equal to” (!=) operator is a comparison operator used in programming languages to determine whether two values or expressions are not equal.

Example of Not equal to (!=) Comparison Operator:

Here are the example of Not equal to (!=) Operator in different language:

C++
#include <iostream>
using namespace std;

int main()
{
    int x = 5;
    int y = 10;
    // x != y returns true if x is not equal to y else it
    // returns false
    if (x != y) {
        cout << "x is not equal to y" << endl;
    }
    else {
        cout << "x is equal to y" << endl;
    }
    return 0;
}
C
#include <stdio.h>
int main()
{
    int x = 5;
    int y = 10;
    // x != y returns true if x is not equal to y else it
    // returns false
    if (x != y) {
        printf("x is not equal to y\n");
    }
    else {
        printf("x is equal to y\n");
    }
    return 0;
}
Java
public class Main {
    public static void main(String[] args)
    {
        int x = 5;
        int y = 10;
        // x != y returns true if x is not equal to y else
        // it returns false
        if (x != y) {
            System.out.println("x is not equal to y");
        }
        else {
            System.out.println("x is equal to y");
        }
    }
}
Javascript
let x = 5;
let y = 10;
// x != y returns true if x is not equal to y 
// else it returns false
if (x != y) {
    console.log("x is not equal to y");
} else {
    console.log("x is equal to y");
}
Python3
x = 5
y = 10
# x != y returns true if x is not equal to y else it returns false
if x != y:
    print("x is not equal to y")
else:
    print("x is equal to y")

Output
x is not equal to y

Greater than (>) Comparison Operator:

The “greater than” (>) operator is a comparison operator used in programming languages to determine whether the value or expression on the left of the operand is greater than the value or expression on the right.

Example of Greater than (>) Comparison Operator:

Here are the example of Greater than (>) Operator in different language:

C++
#include <iostream>
using namespace std;

int main()
{
    int x = 5;
    int y = 10;
    if (x > y) {
       cout << "x is greater than y" << endl;
    }
    else {
        cout << "x is not greater than y" << endl;
    }
    return 0;
}
C
#include <stdio.h>
int main()
{
    int x = 5;
    int y = 10;
    if (x > y) {
        printf("x is greater than y\n");
    }
    else {
        printf("x is not greater than y\n");
    }
    return 0;
}
Java
public class Main {
    public static void main(String[] args)
    {
        int x = 5;
        int y = 10;
        if (x > y) {
            System.out.println("x is greater than y");
        }
        else {
            System.out.println("x is not greater than y");
        }
    }
}
Javascript
let x = 5;
let y = 10;
if (x > y) {
    console.log("x is greater than y");
} else {
    console.log("x is not greater than y");
}
Python3
x = 5
y = 10
if x > y:
    print("x is greater than y")
else:
    print("x is not greater than y")

Output
x is not greater than y

Greater than or equal to (>=) Comparison Operator:

The “greater than or equal to” (>=) operator is a comparison operator used in programming languages to determine whether the value or expression on the left of the operand is greater than or equal to the value or expression on the right.

Example of Greater than or equal to (>=) Comparison Operator:

Here are the example of Greater than or equal to (>=) Operator in different language:

C++
#include <iostream>
using namespace std;

int main()
{
    int x = 5;
    int y = 5;
    if (x >= y) {
       cout << "x is greater than or equal to y"
                  << endl;
    }
    else {
        cout << "x is not greater than or equal to y"
                  << endl;
    }
    return 0;
}
C
#include <stdio.h>
int main()
{
    int x = 5;
    int y = 5;
    if (x >= y) {
        printf("x is greater than or equal to y\n");
    }
    else {
        printf("x is not greater than or equal to y\n");
    }
    return 0;
}
Java
public class Main {
    public static void main(String[] args)
    {
        int x = 5;
        int y = 5;
        if (x >= y) {
            System.out.println(
                "x is greater than or equal to y");
        }
        else {
            System.out.println(
                "x is not greater than or equal to y");
        }
    }
}
Javascript
let x = 5;
let y = 5;
if (x >= y) {
    console.log("x is greater than or equal to y");
} else {
    console.log("x is not greater than or equal to y");
}
Python3
x = 5
y = 5
if x >= y:
    print("x is greater than or equal to y")
else:
    print("x is not greater than or equal to y")

Output
x is greater than or equal to y

Less than (<) Comparison Operator:

The “less than” (<) operator is a comparison operator used in programming languages to determine whether the value or expression on the left of the operand is less than the value or expression on the right.

Example of Less than (<) Comparison Operator:

Here are the example of Less than (<) Operator in different language:

C++
#include <iostream>
using namespace std;

int main()
{
    int x = 5;
    int y = 10;
    if (x < y) {
       cout << "x is less than y" << endl;
    }
    else {
        cout << "x is not less than y" << endl;
    }
    return 0;
}
C
#include <stdio.h>
int main()
{
    int x = 5;
    int y = 10;
    if (x < y) {
        printf("x is less than y\n");
    }
    else {
        printf("x is not less than y\n");
    }
    return 0;
}
Java
public class Main {
    public static void main(String[] args)
    {
        int x = 5;
        int y = 10;
        if (x < y) {
            System.out.println("x is less than y");
        }
        else {
            System.out.println("x is not less than y");
        }
    }
}
Javascript
let x = 5;
let y = 10;
if (x < y) {
    console.log("x is less than y");
} else {
    console.log("x is not less than y");
}
Python3
x = 5
y = 10
if x < y:
    print("x is less than y")
else:
    print("x is not less than y")

Output
x is less than y

Less than or equal to (<=) Comparison Operator:

The “less than or equal to” (<=) operator is a comparison operator used in programming languages to determine whether the value or expression on the left of the operand is less than or equal to the value or expression on the right.

Example of Less than or equal to (<=) Comparison Operator:

Here are the example of Less than or equal to (<=) Operator in different language:

C++
#include <iostream>
using namespace std;

int main()
{
    int x = 5;
    int y = 5;
    if (x <= y) {
        cout << "x is less than or equal to y"
                  << endl;
    }
    else {
     cout << "x is neither less than nor equal to y"
                  << endl;
    }
    return 0;
}
C
#include <stdio.h>
int main()
{
    int x = 5;
    int y = 5;
    if (x <= y) {
        printf("x is less than or equal to y\n");
    }
    else {
        printf("x is neither less than nor equal to y\n");
    }
    return 0;
}
Java
public class Main {
    public static void main(String[] args)
    {
        int x = 5;
        int y = 5;
        if (x <= y) {
            System.out.println(
                "x is less than or equal to y");
        }
        else {
            System.out.println(
                "x is neither less than nor equal to y");
        }
    }
}
Javascript
let x = 5;
let y = 5;
if (x <= y) {
    console.log("x is less than or equal to y");
} else {
    console.log("x is neither less than nor equal to y");
}
Python3
x = 5
y = 5
if x <= y:
    print("x is less than or equal to y")
else:
    print("x is neither less than nor equal to y")

Output
x is less than or equal to y

Conclusion:

Comparison operators are like tools that programmers use to compare things in their code. They help decide if something is true or false, which is super important for making decisions in programs. By knowing how to use these comparison operators properly, programmers can write code that works better and is easier to manage, no matter what kind of programming they’re doing.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads