• Home
  • 3.9 Comparison Operators

3.9 Comparison Operators

View Categories

3.9 Comparison Operators

1 min read

Table of Contents

Comparison operators are relational operators used to compare the relative values of two operands. They determine whether one operand is greater than, less than, greater than or equal to, or less than or equal to another operand. The result of each comparison is a Boolean value.

Comparison operators are commonly used in conditional statements, loops, and decision-making expressions.

Source Code #

#include <iostream>

int main()
{
    int num1 = 25;
    int num2 = 30;

    std::cout << "num1 < num2  : " << (num1 < num2) << '\n';
    std::cout << "num1 > num2  : " << (num1 > num2) << '\n';
    std::cout << "num1 <= num2 : " << (num1 <= num2) << '\n';
    std::cout << "num1 >= num2 : " << (num1 >= num2) << '\n';

    return 0;
}

Output #

num1 < num2  : 1
num1 > num2  : 0
num1 <= num2 : 1
num1 >= num2 : 0

Explanation #

  • The less-than operator (<) returns true if the left operand is smaller than the right operand.
  • The greater-than operator (>) returns true if the left operand is greater than the right operand.
  • The less-than-or-equal-to operator (<=) returns true if the left operand is less than or equal to the right operand.
  • The greater-than-or-equal-to operator (>=) returns true if the left operand is greater than or equal to the right operand.
  • All comparison operators produce a result of type bool.
  • By default, std::cout displays Boolean values as 1 for true and 0 for false.
  • Comparison operators evaluate expressions without modifying either operand.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *