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:

C++

Live example program:

C++

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:

C++

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:

C++

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:

C++

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:

C++

Pro tip: Use ++i or i++ alone on a line when you just want to increase a counter — both do the same:

C++

6. Ternary (Conditional) Operator ?:

A short if-else in one line. Very elegant!

Syntax:

C++

Example:

C++

Another nice one:

C++

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:

C++

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:

C++

9. Full Example Program – Putting It All Together

C++

Your Mini Homework (Try These!)

  1. 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)
  2. Create a variable counter = 0, increment it 5 times using different styles (++c, c++, c += 1), and print after each.
  3. 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! 🚀

You may also like...

Leave a Reply

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