Relational Operators (Comparison Operators) in c++

Relational Operators (also known as Comparison Operators)in C++

There is often need to compare two values in program. Such as whether the value in one a variable is greater than the value in the other variable. Depending upon the situation we can perform different operation for different cases.
Relational Operators (also known as Comparison Operators) are used to compare two values. The result of comparison is a boolean value.
The boolean value can either be true or false. In c and c++, 1 represents true and 0 represent false. However in c++ the data type bool has been introduced, which holds only two values either true or false. The value true in bool corresponds to 1 and 0 corresponds to false.

List of Relational Operators (aka Comparison Operators)


OperatorOperator's NameExampleResult
<less than5<101(true)
<=less than or equal to5<=101(true)
>greater than5>100(false)
>=greater than or equal to5>=100(false)
==equal to5==100(false)
!=not equal to5!=21(true)

/* A c++ program example to demonstrate that the result of comparison is always a boolean value, either 1 or 0. The value 1 represents true and 0 represents false. */



#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    cout << "5 <10" << setw (4) << ": " << (5 < 10) << endl;
    cout << "5 <=10" << setw(3) << ": " << (5 <= 10) << endl;
    cout << "5 >10" << setw (4) << ": " << (5 > 10) << endl;
    cout << "5 >= 10" << setw (2) << ": " << (5 >= 10) << endl;
    cout << "5 == 10" << setw (2) << ": " << (5 == 10) << endl;
    cout << "5 != 10" << setw (2) << ": " << (5 != 10) << endl;

    return 0;
}


This chapter is important to understand selection and looping statement. In coming chapters we will learn about selection and looping statement.

Check out this stream