C++ Operators

Operators in C++ are symbols that perform actions on values and variables. They let you calculate numbers, assign values, compare results, and combine true-or-false conditions.

You have already used operators such as = to assign a value and << to send output to std::cout. This lesson focuses on the operators beginners use most often in expressions.

Arithmetic Operators

Arithmetic operators work with numbers. They can add, subtract, multiply, divide, and find a remainder.

Operator Meaning Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Remainder a % b

The remainder operator % works with integer values. For example, 17 % 5 gives 2 because 5 fits into 17 three times with 2 left over.

Example

#include <iostream>

int main(void) {
    int a = 17;
    int b = 5;

    std::cout << "Sum: " << a + b << std::endl;
    std::cout << "Difference: " << a - b << std::endl;
    std::cout << "Product: " << a * b << std::endl;
    std::cout << "Integer division: " << a / b << std::endl;
    std::cout << "Remainder: " << a % b << std::endl;

    return 0;
}

Output:

Sum: 22
Difference: 12
Product: 85
Integer division: 3
Remainder: 2

How It Works

The variables a and b are both int values. Because both sides of a / b are integers, C++ performs integer division and removes the decimal part. The result is 3, not 3.4.

If you need a decimal result, use a decimal type such as double. For example, dividing 17.0 by 5.0 would produce 3.4.

Assignment Operators

The assignment operator = stores a value in a variable. C++ also has compound assignment operators that update a variable using its current value.

Operator Example Same As
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3

These operators are common when a value changes step by step, such as adding points to a score or reducing an amount.

Comparison Operators

Comparison operators test two values and produce a bool result: true or false.

Operator Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Use == when comparing values. A single = assigns a value instead of checking equality.

Logical Operators

Logical operators combine or change boolean expressions. && means both conditions must be true, || means at least one condition must be true, and ! means not.

#include <iostream>

int main(void) {
    int score = 85;
    int attempts = 2;

    bool passed = score >= 70;
    bool canRetry = attempts < 3;
    bool needsHelp = score < 60 || attempts >= 3;

    score += 5;

    std::cout << "Passed: " << passed << std::endl;
    std::cout << "Can retry: " << canRetry << std::endl;
    std::cout << "Needs help: " << needsHelp << std::endl;
    std::cout << "Updated score: " << score << std::endl;

    return 0;
}

Output:

Passed: 1
Can retry: 1
Needs help: 0
Updated score: 90

Common Mistakes

  • Using = when you meant == in a comparison.
  • Expecting integer division to keep the decimal part.
  • Using % with decimal numbers instead of integers.
  • Forgetting that && requires both conditions to be true.

Takeaway: operators are the building blocks of expressions, and the type of each value affects the result you get.