Operators in C++

Operators are special symbols that perform operations on values and variables. Think of operators as the verbs of a programming language — they do things. C++ has a rich set of operators grouped into several categories.

1. Arithmetic Operators

These operators perform basic mathematical operations:

OperatorNameExampleResult
+Addition5 + 38
-Subtraction9 - 45
*Multiplication3 * 412
/Division10 / 25
%Modulus (remainder)10 % 31
#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 3;
    cout << "Add: "      << a + b << endl;
    cout << "Subtract: " << a - b << endl;
    cout << "Multiply: " << a * b << endl;
    cout << "Divide: "   << a / b << endl;
    cout << "Modulus: "  << a % b << endl;
    return 0;
}

Output:

Add: 13
Subtract: 7
Multiply: 30
Divide: 3
Modulus: 1

2. Relational (Comparison) Operators

These compare two values and return true (1) or false (0):

OperatorMeaningExample
==Equal to5 == 5 → true
!=Not equal to5 != 3 → true
>Greater than7 > 3 → true
<Less than2 < 9 → true
>=Greater than or equal5 >= 5 → true
<=Less than or equal4 <= 6 → true
int x = 10, y = 20;
cout << (x == y) << endl;  // 0 (false)
cout << (x < y)  << endl;  // 1 (true)

3. Logical Operators

Logical operators combine two or more conditions:

OperatorNameDescription
&&ANDTrue only if both conditions are true
||ORTrue if at least one condition is true
!NOTReverses the condition (true becomes false)
int age = 20;
bool hasID = true;

if (age >= 18 && hasID) {
    cout << "Entry allowed" << endl;
}

Output:

Entry allowed

4. Assignment Operators

Assignment operators store values in variables. The basic one is =, but there are shorthand versions:

OperatorExampleEquivalent To
=x = 5Assign 5 to x
+=x += 3x = x + 3
-=x -= 2x = x - 2
*=x *= 4x = x * 4
/=x /= 2x = x / 2
%=x %= 3x = x % 3
int score = 100;
score += 10;
cout << score;  // 110

5. Increment and Decrement Operators

These operators increase or decrease a value by 1:

  • ++ — Increment (add 1)
  • -- — Decrement (subtract 1)

They can appear before (prefix) or after (postfix) the variable:

int a = 5;
cout << a++ << endl;  // Prints 5, then a becomes 6
cout << ++a << endl;  // a becomes 7, then prints 7

Output:

5
7

6. Bitwise Operators

These operate on individual bits of integer values:

OperatorNameExample
&Bitwise AND5 & 3 = 1
|Bitwise OR5 | 3 = 7
^Bitwise XOR5 ^ 3 = 6
~Bitwise NOT~5 = -6
<<Left shift5 << 1 = 10
>>Right shift5 >> 1 = 2

7. Ternary Operator (Conditional Operator)

The ternary operator is a shorthand for simple if-else conditions:

condition ? value_if_true : value_if_false
int a = 10, b = 20;
int max = (a > b) ? a : b;
cout << "Maximum: " << max << endl;

Output:

Maximum: 20

Operator Precedence

When multiple operators appear in one expression, C++ follows a precedence order (similar to BODMAS in math). Higher precedence operators are evaluated first.

int result = 10 + 3 * 2;   // 3*2 = 6, then 10+6 = 16
cout << result;  // 16

Use parentheses to control evaluation order:

int result = (10 + 3) * 2;  // 13 * 2 = 26
cout << result;  // 26

Key Takeaways

  • Arithmetic operators perform math operations.
  • Relational operators compare values and return true/false.
  • Logical operators combine conditions using AND, OR, NOT.
  • Assignment operators store or update values in variables.
  • The ternary operator is a concise way to write simple if-else logic.
  • Operator precedence determines the order of evaluation in expressions.

Leave a Comment

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