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 (
<) returnstrueif the left operand is smaller than the right operand. - The greater-than operator (
>) returnstrueif the left operand is greater than the right operand. - The less-than-or-equal-to operator (
<=) returnstrueif the left operand is less than or equal to the right operand. - The greater-than-or-equal-to operator (
>=) returnstrueif the left operand is greater than or equal to the right operand. - All comparison operators produce a result of type
bool. - By default,
std::coutdisplays Boolean values as1fortrueand0forfalse. - Comparison operators evaluate expressions without modifying either operand.
return 0;terminates the program successfully.