Module 2 · Variables, Operators and Control Flow — topic 2 of 4
Operators
Arithmetic, relational, logical, assignment, increment, conditional and bitwise operators in C++, precedence, and the C++ cast operators.
Learning objectives
- Predict results of integer division, modulus and increments
- Use logical operators and short-circuit evaluation
- Choose the right named cast
Definition
Operators combine operands into expressions. C++ inherits C's operators, adds the scope-resolution (::), member-pointer and named cast operators, and allows most operators to be overloaded for user-defined types.
Introduction
Most operators behave exactly as in C: 7 / 2 is 3, 7 % 2 is 1, && short-circuits. C++ adds a few of its own and replaces C-style casts with four named casts that say what kind of conversion you intend.
Why it matters
Output-prediction questions in exams test precedence and integer arithmetic.
Named casts make dangerous conversions easy to find in code review.
Detailed explanation
Arithmetic: + - * / %. With integers, / truncates toward zero; % keeps the sign of the left operand.
Relational and logical operators produce bool (true/false) in C++. && and || evaluate the right operand only if needed.
static_cast<T>(x) for ordinary conversions; const_cast to add/remove const; reinterpret_cast to reinterpret bits (rarely needed); dynamic_cast for safe downcasts in class hierarchies.
Precedence, high to low: postfix ++ --, unary, * / %, + -, shifts, relational, equality, &, ^, |, &&, ||, ?:, assignment, comma.
Syntax
- Parenthesise mixed expressions for clarity.
- Prefer
static_castto C-style(double)sum. ==compares,=assigns.
Code examples
Integer and real division
3 2 3.4
bool results
true false false
More examples
Short-circuit and increments
false after 1 call(s) 5 7 7 big
Step-by-step explanation
check(false)makes&&false, so the second call is skipped.i++yields 5, theniis 6;++imakes it 7 and yields 7.2 + 3 * 4is 14, which is greater than 10.
Expected output
Running “Short-circuit and increments” prints exactly:
Practical applications
%drives circular buffers and digit extraction.- Short-circuit guards protect pointer dereferences.
static_castappears whenever integer averages must be computed exactly.
Best practices
- Use named casts.
- Parenthesise bitwise expressions used in conditions.
- Prefer pre-increment
++iin loops (it never makes an unnecessary copy for iterators).
Common mistakes
Casting after dividing
Avoid
Prefer
The division happens in integers before the cast.
Modifying a variable twice
Avoid
Prefer
Before C++17 this was undefined; even where defined it is confusing. Never modify a variable twice in one expression.
Important notes
- C++17 defined the evaluation order of
a = b,<<,>>and some others, butf(i++, i++)argument order is still unspecified.
Key takeaways
- Integer
/truncates; cast one operand for real division. - Logical operators yield
booland short-circuit. - Use
static_castrather than C-style casts.
Try it yourself
- 1Read seconds and print hours, minutes and seconds using
/and%. - 2Predict and check:
int x = 10; std::cout << (x++ * 2) << ' ' << x;.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
8 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhich cast should be used for an ordinary conversion such as int to double?