The Wayback Machine - https://web.archive.org/web/20240824161154/https://www.geeksforgeeks.org/cpp-comparison-operators/
Open In App

C++ Comparison Operators

Last Updated : 27 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Comparison operators are operators used for comparing two elements, these are mostly used with if-else conditions as they return true-false as result.

There are mainly 6 Comparison Operators namely:

  1. Greater than (>)  :  this operator checks whether operand1 is greater than operand2. If the result turns out to be true, it returns true or else returns false. example 5>3 ->returns true
  2. Greater than or equal to (>=)  :  this operator checks whether operand1 is greater than or equal to operand2. If the result turns out to be true, it returns true or else returns false. example 5>=5 ->returns true
  3. Less than (<)  :  this operator checks whether operand1 is lesser than operand2. If the result turns out to be true, it returns true or else returns false. example 3<5 ->returns true
  4. Less than or equal to (< =)  :  this operator checks whether operand1 is lesser than or equal to operand2. If the result turns out to be true, it returns true or else returns false. example 5<=5 ->returns true
  5. Equal to (==)  :  this operator checks whether operand1 is equal to operand2. If the result turns out to be true, it returns true or else returns false. example 5==5 ->returns true
  6. Not Equal to (! =)  :  this operator checks whether operand1 is not equal to operand2. If the result turns out to be true, it returns true or else returns false. example 5!=3 ->returns true

Comparison Operators have only two return values, either true (1) or False (0).

Example Code to cover all 6 Comparison Operators: 

C++




// C++ Program to implement
// comparison operators
#include <iostream>
  
using namespace std;
  
int main()
{
  
    int a, b;
    a = 5, b = 3;
    // example to demonstrate '>' operator
    if (a > b)
        cout << "a is greater than b" << endl;
    else
        cout << "a is not greater than b" << endl;
  
    a = 5, b = 5;
    // example to demonstrate '>=' operator
    if (a >= b)
        cout << "a is greater than or equal to b" << endl;
    else
        cout << "a is not greater than or equal b" << endl;
  
    a = 2, b = 3;
    // example to demonstrate '<' operator
    if (a < b)
        cout << "a is lesser than b" << endl;
    else
        cout << "a is not lesser than b" << endl;
  
    a = 2, b = 3;
    // example to demonstrate '<' operator
    if (a <= b)
        cout << "a is lesser than or equal to b" << endl;
    else
        cout << "a is not lesser than or equal to b"
             << endl;
  
    a = 5, b = 5;
    // example to demonstrate '==' operator
    if (a == b)
        cout << "a is equal to b" << endl;
    else
        cout << "a is not equal to b" << endl;
  
    a = 5, b = 3;
    // example to demonstrate '!=' operator
    if (a != b)
        cout << "a is not equal to b" << endl;
    else
        cout << "a is equal to b" << endl;
  
    return 0;
}


Output

a is greater than b
a is greater than or equal to b
a is lesser than b
a is lesser than or equal to b
a is equal to b
a is not equal to b


Similar Reads

Comparison of Exception Handling in C++ and Java
Both languages use to try, catch and throw keywords for exception handling, and their meaning is also the same in both languages. Following are the differences between Java and C++ exception handling: Java C++ Only throwable objects can be thrown as exceptions.All types can be thrown as exceptions.We can catch Exception objects to catch all kinds o
4 min read
Comparison of static keyword in C++ and Java
Static keyword is used for almost the same purpose in both C++ and Java. There are some differences though. This post covers similarities and differences of static keyword in C++ and Java. Similarities between C++ and Java for Static KeywordStatic data members can be defined in both languages.Static member functions can be defined in both languages
6 min read
Comparison of Java with other programming languages
Java is one of the most popular and widely used programming language and platform. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable and secure. From desktop to web applications, scientific supercomputers to gaming consoles, cell phones to the Internet, Java is used in ev
4 min read
Comparison of Python with Other Programming Languages
Python is an easily adaptable programming language that offers a lot of features. Its concise syntax and open-source nature promote readability and implementation of programs which makes it the fastest-growing programming language in current times. Python has various other advantages which give it an edge over other popular programming languages su
3 min read
Results of comparison operations in C and C++
In C, data type of result of comparison operations is int. For example, see the following program. C/C++ Code #include&lt;stdio.h&gt; int main() { int x = 10, y = 10; printf(&quot;%d \n&quot;, sizeof(x == y)); printf(&quot;%d \n&quot;, sizeof(x &lt; y)); return 0; } Output4 4 Whereas in C++, type of results of comparison operations is bool. For exa
1 min read
How to check whether a number is in the range[low, high] using one comparison ?
This is simple, but interesting programming puzzle. Given three integers, low, high and x such that high &gt;= low. How to check if x lies in range [low, high] or not using single comparison. For example, if range is [10, 100] and number is 30, then output is true and if the number is 5, then output is false for same range. A simple solution is com
3 min read
Ratio Manipulations in C++ | Set 2 (Comparison)
Prerequisite - Ratio Manipulations in C++ | Set 1(Arithmetic)In C++, the &lt;ratio&gt; header file allows us to manipulate ratios using various inbuilt template alias. The header file was introduced from C++11 onwards. In this article, we will be discussing the Comparison of Ratio Manipulations in C++. The following functions are used: ratio_equalr
4 min read
Comparison of boolean data type in C++ and Java
The Boolean data type is one of the primitive data types in both C++ and Java. Although, it may seem to be the easiest of all the data types, as it can have only two values - true or false, but it surely is a tricky one as there are certain differences in its usage in both Java and C++, which if not taken care, can result in an error. The differenc
5 min read
reinterpret_cast in C++ | Type Casting operators
reinterpret_cast is a type of casting operator used in C++. It is used to convert a pointer of some data type into a pointer of another data type, even if the data types before and after conversion are different.It does not check if the pointer type and data pointed by the pointer is same or not. Syntax : data_type *var_name = reinterpret_cast &lt;
3 min read
unordered_set operators in C++ STL
Unordered_set provides two operators in C++ STL. These are: Syntax: 1. (unordered_set &amp;lhs == unordered_set &amp;rhs) 2. (unordered_set &amp;lhs != unordered_set &amp;rhs) These operators are discussed in detail below: unordered_set == operator in C++ STL The ‘==’ is an operator in C++ STL performs equality comparison operation between two unor
5 min read
C++ Pointer Operators
Prerequisite: Pointers in C++ A pointer variable is a variable that stores the address of another variable or in other a pointer variable points to the variable whose address is stored inside it. Syntax: int *pointer_name; There are mainly two types of Pointer operators mainly used: Address of operator (&amp;)The indirection operator/Dereference op
2 min read
C++ Arithmetic Operators
Arithmetic Operators in C++ are used to perform arithmetic or mathematical operations on the operands. For example, ‘+’ is used for addition, ‘-‘ is used for subtraction, ‘*’ is used for multiplication, etc. In simple terms, arithmetic operators are used to perform arithmetic operations on variables and data; they follow the same relationship betwe
3 min read
Input/Output Operators Overloading in C++
Operator Overloading is a part of Polymorphism, which enables the feature because of which we can directly use operators with user-defined classes and objects. To read more about this, refer to the article operator overloading in C++. Input/Output Operators(&gt;&gt;/&lt;&lt;) Overloading in C++ We can't directly use the Input/Output Operators (&gt;
2 min read
C++ Increment and Decrement Operators
Prerequisite: Operators in C++ What is a C++ increment Operator? The C++ increment operator is a unary operator. The symbol used to represent the increment operator is (++). The increment operator increases the value stored by the variable by 1. This operator is used for Numeric values only. There are two types of C++ increment Operator: Pre-Increm
4 min read
C++ Relational Operators
In C++ programming language, we sometimes require to compare values and expressions. This comparison allows us to determine relationships, make decisions, and control the flow of our programs. The relational operators in C++ provide the means to compare values and evaluate conditions. In this article, we will learn about C++ relational operators an
4 min read
C++ Logical Operators
In C++ programming languages, logical operators are symbols that allow you to combine or modify conditions to make logical evaluations. They are used to perform logical operations on boolean values (true or false). In C++, there are three logical operators: Logical AND ( &amp;&amp; ) OperatorLogical OR ( || ) OperatorLogical NOT ( ! ) OperatorLet's
4 min read
Unary Operators In C++
Unary operators in C++ are those operators that work on a single value (operand). They perform operations like changing a value's sign, incrementing or decrementing it by one, or obtaining its address. Examples include ++ for increment, -- for decrement, + for positive values, - for negative values,! for logical negation, and &amp; for retrieving m
8 min read
Assignment Operators In C++
In C++, the assignment operator forms the backbone of many algorithms and computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language that is used to assign some value to the variables in C++ or in other wor
7 min read
What are the Operators that Can be and Cannot be Overloaded in C++?
There are various ways to overload Operators in C++ by implementing any of the following types of functions: 1) Member Function 2) Non-Member Function 3) Friend Function List of operators that can be overloaded are: + - * ? % ? &amp; | ~ ! = &lt; &gt; += -= *= ?= %= ?= &amp;= |= &lt;&lt; &gt;&gt; &lt;&lt;= &gt;&gt;= == != &lt;= &gt;= &amp;&amp; ||
4 min read
Increment (Decrement) operators require L-value Expression
What will be the output of the following program? #include&lt;stdio.h&gt; int main() { int i = 10; printf(&quot;%d&quot;, ++(-i)); return 0; } A) 11 B) 10 C) -9 D) None Answer: D, None - Compilation Error. Explanation: In C/C++ the pre-increment (decrement) and the post-increment (decrement) operators require an L-value expression as operand. Provi
1 min read
Order of operands for logical operators
The order of operands of logical operators &amp;&amp;, || are important in C/C++. In mathematics, logical AND, OR, etc... operations are commutative. The result will not change even if we swap RHS and LHS of the operator. In C/C++ (may be in other languages as well)  even though these operators are commutative, their order is critical. For example
1 min read
const_cast in C++ | Type Casting operators
C++ supports following 4 types of casting operators: 1. const_cast 2. static_cast 3. dynamic_cast 4. reinterpret_cast 1. const_cast const_cast is used to cast away the constness of variables. Following are some interesting facts about const_cast. 1) const_cast can be used to change non-const class members inside a const member function. Consider th
4 min read
Overloading stream insertion (&lt;&gt;) operators in C++
In C++, stream insertion operator "&lt;&lt;" is used for output and extraction operator "&gt;&gt;" is used for input. We must know the following things before we start overloading these operators. 1) cout is an object of ostream class and cin is an object of istream class 2) These operators must be overloaded as a global function. And if we want to
2 min read
What are the differences between bitwise and logical AND operators in C/C++?
A Bitwise And operator is represented as '&amp;' and a logical operator is represented as '&amp;&amp;'. The following are some basic differences between the two operators. a) The logical and operator '&amp;&amp;' expects its operands to be boolean expressions (either 1 or 0) and returns a boolean value. The bitwise and operator '&amp;' work on Inte
4 min read
How to sum two integers without using arithmetic operators in C/C++?
Given two integers a and b, how can we evaluate the sum a + b without using operators such as +, -, ++, --, ...? Method 1 (Using pointers)An interesting way would be: C/C++ Code // May not work with C++ compilers and // may produce warnings in C. // Returns sum of 'a' and 'b' int sum(int a, int b) { char *p = a; return (int)&amp;amp;p[b]; } Despite
4 min read
Execution of printf With ++ Operators in C
Consider the following statement in C and predict its output. printf("%d %d %d", i, ++i, i++); This statement invokes undefined behavior by referencing both 'i' and 'i++' in the argument list. It is not defined in which order the arguments are evaluated. Different compilers may choose different orders. A single compiler can also choose different or
2 min read
Conditionally assign a value without using conditional and arithmetic operators
Given 4 integers a, b, y, and x, where x can assume the values of either 0 or 1 only. The following question is asked: If 'x' is 0, Assign value 'a' to variable 'y' Else (If 'x' is 1) Assign value 'b' to variable 'y'. Note: - You are not allowed to use any conditional operator (including the ternary operator) or any arithmetic operator (+, -, *, /)
6 min read
Written version of Logical operators in C++
Can we use keywords in place of operators in C++ ? Yes, certainly, we can. The ANSI C++ Standard has proposed keywords for several C++ operators . They originated in C in the header at the time when there were keyboards that couldn't type the required symbols like &&, !, || etc. In C++, they became alternate token just like regular tokens, except f
2 min read
Comparing String objects using Relational Operators in C++
If strings are compared using relational operators then, their characters are compared lexicographically according to the current character traits, means it starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character is encountered. Parameters : Two Strings required t
2 min read
Casting Operators in C++
Casting operators are used for type casting in C++. They are used to convert one data type to another. C++ supports four types of casts: static_castdynamic_castconst_castreinterpret_cast1. static_castThe static_cast operator is the most commonly used casting operator in C++. It performs compile-time type conversion and is mainly used for explicit c
5 min read
three90RightbarBannerImg