Comparison Operators in Programming

Comparison operators in programming are used to compare two values or expressions and return a Boolean result indicating the relationship between them. These operators play a crucial role in decision-making and conditional statements. Here are the common comparison operators:

OperatorDescriptionExamples
== (Equal to)Checks if the values on both sides are equal.5 == 5; (evaluates to true)
!= (Not equal to)Checks if the values on both sides are not equal.10 != 5; (evaluates to true)
< (Less than)Tests if the value on the left is less than the value on the right.3 < 7; (evaluates to true)
> (Greater than)Tests if the value on the left is greater than the value on the right.10 > 8; (evaluates to true)
<= (Less than or equal to)Checks if the value on the left is less than or equal to the value on the right.5 <= 5; (evaluates to true)
>= (Greater than or equal to)Checks if the value on the left is greater than or equal to the value on the right.8 >= 8; (evaluates to true)

These operators are extensively used in conditional statements, loops, and decision-making constructs to control the flow of a program based on the relationship between variables or values. Understanding comparison operators is crucial for creating logical and effective algorithms in programming.

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

int main()
{

    int a = 5, b = 5, c = 10;
    if (a == b)
        cout << a << " is equal to " << b << endl;
    if (a != c)
        cout << a << " is not equal to " << c << endl;
    if (a < c)
        cout << a << " is less than " << c << endl;
    if (a <= b)
        cout << a << " is less than or equal to " << b << endl;
    if (c > a)
        cout << c << " is greater than " << a << endl;
    if (a >= b)
        cout << a << " is greater than or equal to " << b << endl;
    return 0;
}
Java
/* package whatever //do not write package name here */

import java.io.*;

class GFG {
    public static void main(String[] args)
    {
        int a = 5, b = 5, c = 10;

        if (a == b)
            System.out.println(a + " is equal to " + b);
        if (a != c)
            System.out.println(a + " is not equal to " + c);
        if (a < c)
            System.out.println(a + " is less than " + c);
        if (a <= b)
            System.out.println(a + " is less than or equal to " + b);
        if (c > a)
            System.out.println(c + " is greater than " + a);
        if (a >= b)
            System.out.println(a + " is greater than or equal to " + b);
    }
}
Python3
a = 5
b = 5
c = 10

if a == b:
    print(f"{a} is equal to {b}")
if a != c:
    print(f"{a} is not equal to {c}")
if a < c:
    print(f"{a} is less than {c}")
if a <= b:
    print(f"{a} is less than or equal to {b}")
if c > a:
    print(f"{c} is greater than {a}")
if a >= b:
    print(f"{a} is greater than or equal to {b}")
JavaScript
function main() {
    let a = 5, b = 5, c = 10;

    if (a === b)
        console.log(a + " is equal to " + b);
    if (a !== c)
        console.log(a + " is not equal to " + c);
    if (a < c)
        console.log(a + " is less than " + c);
    if (a <= b)
        console.log(a + " is less than or equal to " + b);
    if (c > a)
        console.log(c + " is greater than " + a);
    if (a >= b)
        console.log(a + " is greater than or equal to " + b);
}

main();

Output
5 is equal to 5
5 is not equal to 10
5 is less than 10
5 is less than or equal to 5
10 is greater than 5
5 is greater than or equal to 5

Types of Operators in Programming

Types of operators in programming are symbols or keywords that represent computations or actions performed on operands. Operands can be variables, constants, or values, and the combination of operators and operands form expressions. Operators play a crucial role in performing various tasks, such as arithmetic calculations, logical comparisons, bitwise operations, etc.

Types of Operators in Programming

Table of Content

  • Types of Operators in Programming
  • Arithmetic Operators in Programming
  • Comparison Operators in Programming
  • Logical Operators in Programming
  • Assignment Operators in Programming
  • Increment and Decrement Operators in Programming
  • Bitwise Operators in Programming

Similar Reads

Types of Operators in Programming:

Here are some common types of operators:...

Arithmetic Operators in Programming:

Arithmetic operators in programming are fundamental components of programming languages, enabling the manipulation of numeric values for various computational tasks. Here’s an elaboration on the key arithmetic operators:...

Comparison Operators in Programming:

Comparison operators in programming are used to compare two values or expressions and return a Boolean result indicating the relationship between them. These operators play a crucial role in decision-making and conditional statements. Here are the common comparison operators:...

Logical Operators in Programming:

Logical operators in programming are used to perform logical operations on Boolean values. These operators are crucial for combining or manipulating conditions and controlling the flow of a program based on logical expressions. Here are the common logical operators:...

Assignment Operators in Programming:

Assignment operators in programming are used to assign values to variables. They are essential for storing and updating data within a program. Here are common assignment operators:...

Increment and Decrement Operators in Programming:

Increment and decrement operators in programming are used to increase or decrease the value of a variable by 1, respectively. They are shorthand notations for common operations and are particularly useful in loops. Here are the two types:...

Bitwise Operators in Programming:

Bitwise operators in programming perform operations at the bit level, manipulating individual bits of binary representations of numbers. These operators are often used in low-level programming, such as embedded systems and device drivers. Here are the common bitwise operators:...