Operators in C++ Part II

Relational Operators

Relational operators are used to check the relationship between two operands and to compare two or more numbers or even expressions in cases. The return type of a relational operator is a Boolean that is, either True or False (1 or 0).

OperatorDescription
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
==Is equal to
!=Is not equal to
#include <iostream>
using namespace std;
 
int main()
{
    int a = 4, b = 5;
    cout << "The value of a == b is " << (a == b) << endl;
    cout << "The value of a < b is " << (a < b) << endl;
    cout << "The value of a > b is " << (a > b) << endl;
}

Output:

The value of a==b is 0
The value of a<b is 1
The value of a>b is 0

Logical Operators

To determine whether an expression is true or false, logical operators are utilised. The logical operators AND, OR, and NOT are the three. Although they are frequently used to compare expressions to determine whether they are satisfactory or not, they may be used to compare Boolean values as well.

  • AND: it returns true when both operands are true or 1.
  • OR: it returns true when either operand is true or 1.
  • NOT: it is employed to change the operand’s logical state and is true when the operand is false.
OperatorDescription
&&AND Operator
||OR Operator
!NOT Operator
#include <iostream>
using namespace std;
 
int main()
{
    int a = 1, b = 0;
    cout << "The value of a && b is " << (a && b) << endl;
    cout << "The value of a || b is " << (a || b) << endl;
    cout << "The value of !a is " << (!a) << endl;
}

Output:

The value of a && b is 0
The value of a || b is 1
The value of !a is 0
Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in