Chapter 4: Operators
Operators in C++: The Tools That Make Your Code “Do Stuff”
Hello again, my brilliant student! 👋 You’ve already mastered variables and data types — fantastic! Now we’re going to learn operators — the symbols that let you perform actions on those variables: add numbers, compare values, check conditions, flip bits, and much more.
Think of operators as the verbs of C++ — they tell the computer what to do with your data.
Today we’ll cover all the important categories in great detail:
- Arithmetic operators (+ – * / %)
- Assignment operators (= += -= *= /= %=)
- Comparison (relational) operators (== != < > <= >=)
- Logical operators (&& || !)
- Increment & decrement (++ –)
- Bitwise operators (& | ^ ~ << >>)
- Ternary (conditional) operator (?:)
- Operator precedence and associativity (super important!)
We’ll go step-by-step with tons of examples, real programs, common mistakes, and tips that professional programmers actually use.
Let’s start coding!
1. Arithmetic Operators
These do basic math — exactly what you expect.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 5 + 3 | 8 |
| – | Subtraction | 10 – 4 | 6 |
| * | Multiplication | 6 * 7 | 42 |
| / | Division | 10 / 3 | 3 (integer division!) |
| % | Modulo (remainder) | 10 % 3 | 1 |
Important rule for / and integers:
|
0 1 2 3 4 5 6 7 |
int a = 10 / 3; // a = 3 (truncates decimal part) double b = 10.0 / 3; // b ≈ 3.33333 (use floating-point!) |
Live example program:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> int main() { int x = 17; int y = 5; std::cout << "x + y = " << x + y << "\n"; // 22 std::cout << "x - y = " << x - y << "\n"; // 12 std::cout << "x * y = " << x * y << "\n"; // 85 std::cout << "x / y = " << x / y << "\n"; // 3 (integer division) std::cout << "x % y = " << x % y << "\n"; // 2 (remainder) double z = 17.0 / 5; // Use .0 to force floating-point division std::cout << "17.0 / 5 = " << z << "\n"; // 3.4 return 0; } |
2. Assignment Operators
Used to assign values to variables. The basic one is =, but there are compound versions that combine operation + assignment.
| Operator | Meaning | Equivalent to |
|---|---|---|
| = | Assign | x = 10; |
| += | Add and assign | x += 5; → x = x + 5; |
| -= | Subtract and assign | x -= 3; |
| *= | Multiply and assign | x *= 2; |
| /= | Divide and assign | x /= 4; |
| %= | Modulo and assign | x %= 7; |
Very common pattern:
|
0 1 2 3 4 5 6 7 8 9 |
int score = 0; score += 10; // score is now 10 score += 20; // score is now 30 score *= 2; // score is now 60 |
3. Comparison (Relational) Operators
These compare two values and return true or false (i.e., a bool).
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | true |
| != | Not equal to | 5 != 3 | true |
| < | Less than | 4 < 10 | true |
| > | Greater than | 7 > 2 | true |
| <= | Less than or equal | 5 <= 5 | true |
| >= | Greater than or equal | 10 >= 8 | true |
Example:
|
0 1 2 3 4 5 6 7 8 |
int age = 20; bool canVote = (age >= 18); // true bool isTeen = (age >= 13 && age <= 19); // true |
4. Logical Operators
Combine or modify boolean expressions.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| && | AND | (age >= 18) && (hasID == true) | Both must be true |
|
OR | ||
| ! | NOT | !isRaining | Reverses true/false |
Classic example:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
bool hasTicket = true; bool isAdult = true; bool canEnter = hasTicket && isAdult; // true bool isWeekend = false; bool canSleepLate = isWeekend || !isAdult; // true (because !isAdult is false, but isWeekend is false → wait, let's check!) |
5. Increment & Decrement Operators (++ and –)
Super common — especially in loops!
| Operator | Meaning | Example |
|---|---|---|
| ++x (pre) | Increment first, then use value | int a = 5; int b = ++a; → a=6, b=6 |
| x++ (post) | Use value first, then increment | int a = 5; int b = a++; → a=6, b=5 |
| –x (pre) | Decrement first | Same logic |
| x– (post) | Use first, then decrement | Same logic |
Very frequent mistake beginners make:
|
0 1 2 3 4 5 6 7 8 |
int i = 5; std::cout << i++ << "\n"; // prints 5, then i becomes 6 std::cout << ++i << "\n"; // prints 7 (i was 6, incremented to 7) |
Pro tip: Use ++i or i++ alone on a line when you just want to increase a counter — both do the same:
|
0 1 2 3 4 5 6 7 |
i++; // fine ++i; // also fine (and sometimes slightly faster) |
6. Ternary (Conditional) Operator ?:
A short if-else in one line. Very elegant!
Syntax:
|
0 1 2 3 4 5 6 |
condition ? valueIfTrue : valueIfFalse; |
Example:
|
0 1 2 3 4 5 6 7 8 |
int age = 20; std::string status = (age >= 18) ? "Adult" : "Minor"; std::cout << "You are a(n) " << status << ".\n"; // You are a(n) Adult. |
Another nice one:
|
0 1 2 3 4 5 6 7 |
int a = 10, b = 25; int max = (a > b) ? a : b; // max = 25 |
7. Bitwise Operators (Quick Overview – Useful Later!)
These work on the binary (0s and 1s) representation of integers.
| Operator | Meaning | Example |
|---|---|---|
| & | Bitwise AND | 5 & 3 → 1 |
|
Bitwise OR | |
| ^ | Bitwise XOR | 5 ^ 3 → 6 |
| ~ | Bitwise NOT (flip) | ~5 → -6 |
| << | Left shift | 5 << 1 → 10 |
| >> | Right shift | 20 >> 2 → 5 |
Common real use: Fast multiplication/division by powers of 2:
|
0 1 2 3 4 5 6 7 8 |
int x = 10; x = x << 1; // x = 20 (multiply by 2) x = x >> 1; // x = 10 (divide by 2) |
8. Operator Precedence & Associativity – The Order That Matters!
C++ doesn’t read left-to-right blindly — it follows rules (just like math: × before +).
Quick precedence table (highest to lowest):
| Priority | Operators | Associativity |
|---|---|---|
| 1 | ++ — (postfix), function calls | Left to right |
| 2 | ++ — (prefix), ! ~ (unary) | Right to left |
| 3 | * / % | Left to right |
| 4 | + – | Left to right |
| 5 | << >> | Left to right |
| 6 | < <= > >= | Left to right |
| 7 | == != | Left to right |
| 8 | & (bitwise) | Left to right |
| 9 | ^ | Left to right |
| 10 | |
|
| 11 | && | Left to right |
| 12 | |
|
| 13 | ?: (ternary) | Right to left |
| 14 | = += -= *= /= %= <<= >>= &= ^= | =` |
Golden rule for beginners: When in doubt, use parentheses () — they always win and make code clearer!
Example of confusion:
|
0 1 2 3 4 5 6 7 8 9 10 |
int x = 5 + 3 * 2; // 3*2 first → 5+6 → 11 int y = (5 + 3) * 2; // 8*2 → 16 bool a = true || false && false; // && first → true || false → true bool b = (true || false) && false; // true && false → false |
9. Full Example Program – Putting It All Together
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <iostream> #include <string> int main() { int a = 10; int b = 3; std::cout << "a = " << a << ", b = " << b << "\n\n"; // Arithmetic std::cout << "a + b = " << a + b << "\n"; std::cout << "a % b = " << a % b << "\n"; // Compound assignment a += 5; // a = 15 std::cout << "After a += 5 → a = " << a << "\n"; // Increment/Decrement std::cout << "a++ = " << a++ << " (now a = " << a << ")\n"; // 15, then a=16 std::cout << "++a = " << ++a << "\n"; // 17 // Comparison & Logical bool isBig = (a > 10) && (b < 5); // true && true → true std::cout << "Is a big and b small? " << (isBig ? "Yes" : "No") << "\n"; // Ternary std::string message = (a >= 18) ? "Adult" : "Young"; std::cout << "Message: " << message << "\n"; return 0; } |
Your Mini Homework (Try These!)
- Write a program that asks the user for two numbers and prints:
- Sum, difference, product, quotient (use double for division!)
- Which one is larger (using ternary)
- Create a variable counter = 0, increment it 5 times using different styles (++c, c++, c += 1), and print after each.
- Write: int weird = 10 + 5 * 2 – 3 / 3; Add parentheses in different ways and see how the result changes.
You’re doing incredibly well! Operators are like the muscles of your code — now you know how to make things move, compare, and decide.
Next lesson: Input/Output in detail + basic control structures (if, else, switch).
Any questions? Confused about precedence? Want more examples with ++ or ternary? Just ask — your friendly C++ teacher is right here! 🚀
