Conditional Operators in if Statements

Conditional operators are used to compare two values and return a boolean value (true or false). The most common conditional operators are:

  • == (equal to)
  • != (not equal to)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)

Example:

C++




#include <iostream>
 
using namespace std;
 
int main()
{
    int a = 5, b = 10;
 
    // Equal to
    if (a == b) {
        cout << "a is equal to b" << endl;
    }
 
    // Not equal to
    if (a != b) {
        cout << "a is not equal to b" << endl;
    }
 
    // Greater than
    if (a > b) {
        cout << "a is greater than b" << endl;
    }
 
    // Less than
    if (a < b) {
        cout << "a is less than b" << endl;
    }
 
    // Greater than or equal to
    if (a >= b) {
        cout << "a is greater than or equal to b" << endl;
    }
 
    // Less than or equal to
    if (a <= b) {
        cout << "a is less than or equal to b" << endl;
    }
 
    return 0;
}


C




#include <stdio.h>
 
int main()
{
    int a = 5, b = 10;
 
    // Equal to
    if (a == b) {
        printf("a is equal to b\n");
    }
 
    // Not equal to
    if (a != b) {
        printf("a is not equal to b\n");
    }
 
    // Greater than
    if (a > b) {
        printf("a is greater than b\n");
    }
 
    // Less than
    if (a < b) {
        printf("a is less than b\n");
    }
 
    // Greater than or equal to
    if (a >= b) {
        printf("a is greater than or equal to b\n");
    }
 
    // Less than or equal to
    if (a <= b) {
        printf("a is less than or equal to b\n");
    }
 
    return 0;
}


Java




public class Main {
    public static void main(String[] args)
    {
        int a = 5, b = 10;
 
        // Equal to
        if (a == b) {
            System.out.println("a is equal to b");
        }
 
        // Not equal to
        if (a != b) {
            System.out.println("a is not equal to b");
        }
 
        // Greater than
        if (a > b) {
            System.out.println("a is greater than b");
        }
 
        // Less than
        if (a < b) {
            System.out.println("a is less than b");
        }
 
        // Greater than or equal to
        if (a >= b) {
            System.out.println(
                "a is greater than or equal to b");
        }
 
        // Less than or equal to
        if (a <= b) {
            System.out.println(
                "a is less than or equal to b");
        }
    }
}


Python




a = 5
b = 10
 
# Equal to
if a == b:
    print("a is equal to b")
 
 
# Not equal to
if a != b:
    print("a is not equal to b")
 
 
# Greater than
if a > b:
    print("a is greater than b")
 
 
# Less than
if a < b:
    print("a is less than b")
 
 
# Greater than or equal to
if a >= b:
    print("a is greater than or equal to b")
 
 
# Less than or equal to
if a <= b:
    print("a is less than or equal to b")


C#




using System;
 
class Program {
    static void Main()
    {
        int a = 5, b = 10;
 
        // Equal to
        if (a == b) {
            Console.WriteLine("a is equal to b");
        }
 
        // Not equal to
        if (a != b) {
            Console.WriteLine("a is not equal to b");
        }
 
        // Greater than
        if (a > b) {
            Console.WriteLine("a is greater than b");
        }
 
        // Less than
        if (a < b) {
            Console.WriteLine("a is less than b");
        }
 
        // Greater than or equal to
        if (a >= b) {
            Console.WriteLine(
                "a is greater than or equal to b");
        }
 
        // Less than or equal to
        if (a <= b) {
            Console.WriteLine(
                "a is less than or equal to b");
        }
    }
}


Javascript




// Define variables a and b
let a = 5, b = 10;
 
// Equal to
if (a === b) {
    console.log("a is equal to b");
}
 
// Not equal to
if (a !== b) {
    console.log("a is not equal to b");
}
 
// Greater than
if (a > b) {
    console.log("a is greater than b");
}
 
// Less than
if (a < b) {
    console.log("a is less than b");
}
 
// Greater than or equal to
if (a >= b) {
    console.log("a is greater than or equal to b");
}
 
// Less than or equal to
if (a <= b) {
    console.log("a is less than or equal to b");
}


Output

a is not equal to b
a is less than b
a is less than or equal to b


If statement in Programming

An if statement is a fundamental control structure in programming languages that allows you to execute specific code blocks based on whether a condition is true or false. It is used to make decisions and control the flow of execution in your program.

If statement in Programming

Table of Content

  • What is an If Statement?
  • Example of If Statement in Programming
  • Conditional Operators in If Statements
  • Common Mistakes to Avoid
  • Best Practices for Using If Statements

Similar Reads

What is an if Statement?

The if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not....

if Statement Syntax:

The syntax of the “if” statement varies slightly across different languages, but the general syntax of an if statement in most programming languages is as follows:...

Example of if Statement in Programming:

...

Conditional Operators in if Statements

Let’s explore examples of “if” statements in various programming languages:...

Common Mistakes to Avoid:

...

Best Practices for Using if Statements:

...

Contact Us