Broad Network


Equality and Relational Operators in C++

C++ Operators – Part 3

Forward: In this part of the series we look at equality and relational operators in C++.

By: Chrysanthus Date Published: 23 Aug 2012

Introduction

This is part 3 of my series, C++ Operators. In this part of the series we look at equality and relational operators in C++. Equality operators are the == and != operators (see explanation below). Relational operators are the <, >, <= and >= operators (see explanation below).

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The Equal Operator
It is ==, typed as a double assignment operator. The equal operator returns true if the operands on either sides are equal, otherwise it returns false.

The Not Equal Operator
The Not Equal operator is the opposite of the Equal Operator. The Not Equal operator is, != . It returns true if the operands are not equal, otherwise it returns false. Let us look at some examples:

Try the following code:

#include <iostream>
using namespace std;

int main()
    {

        int myIdent = 25;
        int hisIdent = 30;
        if (myIdent != hisIdent)
            {
                cout << "The values of the two objects are not equal.";
            }

        return 0;
    }

myIdent is 25, hisIdent is 30. The condition is read like this: If myIdent is not equal to hisIdent, then the if-block will be executed. Since the values of the objects are not equal,  (myIdent != myIdent) returns true.

In the following code, the values of the two objects are equal, so the condition returns false and the if-block is not executed.

#include <iostream>
using namespace std;

int main()
    {

        int myIdent = 50;
        int hisIdent = 50;
        if (myIdent != hisIdent)
            {
                cout << "The values of the two objects are not equal.";
            }

        return 0;
    }

The Greater Than Operator
The Greater Than operator is, > . It returns true if the left operand is greater than the right operand. In the following example, the left operand is greater than the right operand. So the if-block is executed:

#include <iostream>
using namespace std;

int main()
    {

        int ident1 = 60;
        int ident2 = 70;
        if (ident2 > ident1)
            {
                cout << "The value of ident2 is greater than the value of ident1.";
            }


        return 0;
    }

Read and try the above code.

Greater Than Or Equal Operator
The Greater Than or Equal operator is, >= (it is the math greater than sign followed by the math equal sign). It returns true if the left operand is greater than or equal to the right operand.

The Less Than Operator
The Less Than Operator is < .It returns true if the left operand is less than the right operand.

The Less Than or Equal Operator
The Less than or Equal operator is, <= . It returns true if the left operand is less than or equal to the right operand.

The equality and relational operators are left-to-right operators.

That is it for equality and relational operators. Let us take a break here. Rendezvous in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message