Example of C++ Relational Operators

In the below code, we have defined two variables with some integer value and we have printed the output by comparing them using relational operators in C++. In the output, we get 1, 0, 0, 0, and 1 where 0 means false and 1 means true.

C++




// C++ Program to illustrate the relational operators
#include <iostream>
using namespace std;
  
int main()
{
    // variables for comparison
    int a = 10;
    int b = 6;
  
    // greater than
    cout << "a > b = " << (a > b) << endl;
    
    // less than
    cout << "a < b = " << (a < b) << endl;
    
    // equal to
    cout << "a == b = " << (a == b) << endl;
    
    // not equal to
    cout << "a != b = " << (a != b) << endl;
  
    return 0;
}


Output

a > b = 1
a < b = 0
a == b = 0
a != b = 1





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 and understand their significance in making logical comparisons in code.

Similar Reads

Relational Operators in C++

C++ Relational operators are used to compare two values or expressions, and based on this comparison, it returns a boolean value (either true or false) as the result. Generally false is represented as 0 and true is represented as any non-zero value (mostly 1)....

Types of C++ Relational Operators

We have six relational operators in C++ which are explained below with examples....

Example of C++ Relational Operators

In the below code, we have defined two variables with some integer value and we have printed the output by comparing them using relational operators in C++. In the output, we get 1, 0, 0, 0, and 1 where 0 means false and 1 means true....