• Home
  • 3.8 Equality Operators

3.8 Equality Operators

View Categories

3.8 Equality Operators

< 1 min read

Table of Contents

Equality operators are relational operators used to compare two values. They determine whether two operands are equal or not equal and produce a Boolean result. The result of an equality comparison is either true or false.

The equality operator (==) checks whether two operands have the same value, while the inequality operator (!=) checks whether the operands have different values.

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';

    return 0;
}

Output #

num1 == num2 : 0
num1 != num2 : 1

Explanation #

  • The equality operator (==) compares two operands and returns true if their values are equal.
  • The inequality operator (!=) compares two operands and returns true if their values are different.
  • Both operators produce a Boolean result of type bool.
  • By default, std::cout displays Boolean values as 1 for true and 0 for false.
  • Parentheses are used around the comparison expressions to improve readability.
  • Equality operators compare values and do not modify 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 *