In this tutorial, you shall learn about Equal-to Relational Operator in C++ programming language, its syntax, and how to use this operator with the help of examples.

C++ Equal-to Operator

In C++, Equal to Relational Operator is used to check if left operand is equal to second operand. In this tutorial, we will learn how to use the Equal to Operator in C++, with examples.

The syntax to check if x equals y using Equal to Operator is

x == y

The operator returns a boolean value of true if x is equal to y, or false if not.

Examples

ADVERTISEMENT

1. Check if two numbers are equal

In the following example, we take two integer values in x and y, and check if these two are equal, using Equal to Operator.

main.cpp

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    int y = 5;
    
    if (x == y) {
        cout << "x and y are equal." << endl;
    } else {
        cout << "x and y are not equal." << endl;
    }
}

Output

x and y are equal.
Program ended with exit code: 0

Since values in x and y are equal, x == y returned true.

2. Check if two strings are equal

Now, let us take two strings, and check if they are equal using Equal to Operator.

main.cpp

#include <iostream>
using namespace std;

int main() {
    string x = "apple";
    string y = "banana";
    
    if (x == y) {
        cout << "x and y are equal." << endl;
    } else {
        cout << "x and y are not equal." << endl;
    }
}

Output

x and y are not equal.
Program ended with exit code: 0

Since values in x and y are not equal, x == y returned false.

Conclusion

In this C++ Tutorial, we learned about Equal to Operator in C++, with examples.