C++ Operators


Go to main Page

Operators in C++

In this tutorial, we will learn about many operators in C++ with the help of examples.

Operators are symbols that forms operations on variables.

Operators in C++ can be classified into 6 types:

C++ Arithmetic Operators

Arthmetic operators are used to perform arthematic operations.

These operators include *,/,%,+ and -.

Example


#include 
using namespace std;

int main() {
    int x, y;
    x = 10;
    y = 5;

    // displaying the sum of x and y
    cout << "x + y = " << x+y << endl;

    // displaying the difference of x and y
    cout << "x - y = " << x - y << endl;

    // displaying the product of x and y
    cout << "x * y = " << x * y << endl;

    // displaying the division of x by y  
      cout << "x / y = " << x / y << endl;

    return 0;
}

Output


 a + b = 15
 a - b = 5
 a * b = 50
 a / b = 2

2. C++ Assignment Operators

Assignment Operators are used assign the value.The assignment operator is =

//assiging value
    int a=45;

Operator Equvilant to
= a=b;
+= a=a+b;
-= a=a-b;
*= a=a*b;
%= a=a%b;
/= a=a/b;
Example

#include <iostream>
using namespace std;

int main() {
    int a, b, temp;

    // 2 is assigned to a
    a = 2;

    // 7 is assigned to b
    b = 7;

   // value of a is assigned to temp
   temp = a;    // temp will be 2
   cout << "temp = " << temp << endl;

   // assigning the sum of a and b to a
    a += b;       // a = a +b
    cout << "a = " << a << endl;

    return 0;
}

Output

temp = 2
a = 9

3. C++ Relational Operators

A relational operator is used to relate or compare two or more values.

Example

#include <iostream>
using namespace std;

int main() {
    int a, b;
    a = 3;
    b = 5;
    bool result;

    result = (a == b);   // false
    cout << "3 == 5 is " << result << endl;

    result = (a != b);  // true
    cout << "3 != 5 is " << result << endl;

    result = a > b;   // false
    cout << "3 > 5 is " << result << endl;

    result = a < b;   // true
    cout << "3 < 5 is " << result << endl;

    result = a >= b;  // false
    cout << "3 >= 5 is " << result << endl;

    result = a <= b;  // true
    cout << "3 <= 5 is " << result << endl;

    return 0;
}

Output

3 == 5 is 0
3 != 5 is 1
3 > 5 is 0
3 < 5 is 1
3 >= 5 is 0
3 <= 5 is 1

4. C++ Logical Operators

Logical operators are used to check if this logic is true or false.

Example

#include <iostream>
using namespace std;

int main() {
    bool result;

    result = (3 != 5) && (3 < 5);     // true
    cout << "(3 != 5) && (3 < 5) is " << result << endl;

    result = (3 == 5) && (3 < 5);    // false
    cout << "(3 == 5) && (3 < 5) is " << result << endl;

    result = (3 == 5) && (3 > 5);    // false
    cout << "(3 == 5) && (3 > 5) is " << result << endl;

    result = (3 != 5) || (3 < 5);    // true
    cout << "(3 != 5) || (3 < 5) is " << result << endl;

    result = (3 != 5) || (3 > 5);    // true
    cout << "(3 != 5) || (3 > 5) is " << result << endl;

    result = (3 == 5) || (3 > 5);    // false
    cout << "(3 == 5) || (3 > 5) is " << result << endl;

    result = !(5 == 2);    // true
    cout << "!(5 == 2) is " << result << endl;

    result = !(5 == 5);    // false
    cout << "!(5 == 5) is " << result << endl;

    return 0;
}

Output

(3 != 5) && (3 < 5) is 1
(3 == 5) && (3 < 5) is 0
(3 == 5) && (3 > 5) is 0
(3 != 5) || (3 < 5) is 1
(3 != 5) || (3 > 5) is 1
(3 == 5) || (3 < 5) is 0
!(5 == 2) is 1
!(5 == 5) is 0