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 returnstrueif their values are equal. - The inequality operator (
!=) compares two operands and returnstrueif their values are different. - Both operators produce a Boolean result of type
bool. - By default,
std::coutdisplays Boolean values as1fortrueand0forfalse. - 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.