Operators
By the end of this lesson you'll be able to do maths, compare values, combine true/false conditions, and understand the order C++ evaluates an expression — the toolkit behind every calculation and decision your programs make.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
Part of the free C++ course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Think of operators as the buttons on a calculator . The number keys are your variables; the + , - , × and ÷ keys are the arithmetic operators. But C++ has extra buttons a basic calculator lacks: a " compare " button that answers yes or no ( , == ), " and / or " buttons that fold several yes/no answers into one decision, and a " % " button that hands you the remainder. Learn what each button does and in what order the calculator presses them, and you can express any calculation or rule.
1. Arithmetic Operators
The five arithmetic operators are + add, - subtract, * multiply, / divide, and % modulo (remainder). The one that surprises every beginner is / : when both sides are whole numbers, C++ does integer division and silently throws away the decimal part, so 17 / 5 is 3 , not 3.4 . Make one side a decimal ( 17.0 ) to get a real result. Read this worked example and run it.
Your turn. % and integer division together let you split a total into parts — here, seconds into hours, minutes and seconds. Fill in the three blanks marked ___ , then run it.
2. Comparison & Logical Operators
Comparison operators ask a yes/no question and hand back a bool — cout prints that as 1 for true or 0 for false. The six are == equal, != not equal, , , and . Logical operators then combine those answers: && (AND) is true only when both sides are true, || (OR) is true when at least one is, and ! (NOT) flips a value. C++ is also short-circuit : with && a false left side skips the right entirely, which is how you write safe guards.
Now you try. A ride is allowed if you are tall enough and (old enough or with a guardian). Pick the right operator for each blank:
3. Increment, Decrement & Compound Assignment
Adding or subtracting 1 is so common it has its own operators: ++ and -- . Where you put them matters. Post ( x++ ) uses the old value then adds 1; pre ( ++x ) adds 1 then uses the new value. The compound operators ( += , -= , *= , /= , %= ) are shorthand: score += 25 means exactly score = score + 25 , just shorter and clearer.
4. Bitwise Operators
Numbers are stored as bits — strings of 0s and 1s — and the bitwise operators work on those bits directly. & (AND), | (OR), ^ (XOR) and ~ (NOT) combine bits, while the shift operators and slide all the bits left or right. A handy shortcut: shifting left by one ( x 1 ) doubles a number, and shifting right halves it. You won't reach for these every day, but you'll meet them in flags, permissions and performance code.
5. Operator Precedence
Precedence is the order C++ applies operators when you don't use parentheses — exactly like "BODMAS" from school. * and / run before + and - , so 2 + 3 * 4 is 14 , not 20 . Comparisons run before && , which runs before || , and assignment ( = ) runs last. The golden rule: when an expression isn't obvious , add parentheses. They cost nothing and remove all doubt.
When two operators share a level they're read left to right (assignment is the exception — it reads right to left). Don't memorise the whole table; just remember "maths before comparisons before && / || , and parentheses beat everything."
No blanks this time — just a brief and an outline to keep you on track. Use integer division and % to split a pence total into pounds and leftover pence, then run it and check your output against the comment.
Practice quiz
What does 17 % 5 evaluate to?
- 3
- 3.4
- 2
- 1
Answer: 2. % is modulo (remainder): 17 divided by 5 is 3 remainder 2.
What does the int expression 17 / 5 evaluate to?
- 3
- 3.4
- 4
- 2
Answer: 3. Both operands are int, so integer division drops the remainder, giving 3.
What value do comparison operators like == and > return?
- An int count
- A string
- A char
- A bool (printed as 1 or 0)
Answer: A bool (printed as 1 or 0). Comparisons return a bool, which cout prints as 1 (true) or 0 (false).
When is the && (AND) operator true?
- When at least one side is true
- Only when both sides are true
- When both sides are false
- Always
Answer: Only when both sides are true. && is true only when BOTH operands are true.
What does short-circuit evaluation mean for &&?
- If the left side is false, the right side is skipped
- Both sides always run
- The right side runs first
- It throws if the left is false
Answer: If the left side is false, the right side is skipped. With &&, a false left side makes the result false, so the right side is never evaluated.
What is the difference between x++ and ++x?
- No difference ever
- x++ returns the new value, ++x the old
- x++ returns the old value, ++x returns the new value
- ++x subtracts 1
Answer: x++ returns the old value, ++x returns the new value. Post-increment x++ yields the old value then adds 1; pre-increment ++x adds 1 then yields the new value.
What is 'score += 25;' shorthand for?
- score = 25
- score = score + 25
- score = score * 25
- score == 25
Answer: score = score + 25. Compound assignment += means score = score + 25.
What does 5 << 1 evaluate to?
- 6
- 2
- 25
- 10
Answer: 10. Shifting left by 1 doubles the value: 5 << 1 = 10.
Because of precedence, what does 2 + 3 * 4 evaluate to?
- 20
- 14
- 24
- 9
Answer: 14. * binds tighter than +, so 3*4 happens first: 2 + 12 = 14.
What does writing 'if (x = 5)' actually do?
- Compares x with 5
- Causes a runtime crash
- Assigns 5 to x and is always true
- Sets x to 0
Answer: Assigns 5 to x and is always true. A single = assigns; if (x = 5) stores 5 in x and the non-zero value is always true. Use == to compare.