Skip to content

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.

Beginner 14 min read·Lesson 5 of 40 in C++ Programming

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

C++
int q = a / b, r = a % b;
bool both = (x > 0) && (y > 0);
double avg = static_cast<double>(sum) / n;
int big = (a > b) ? a : b;
count += 5;
  • Parenthesise mixed expressions for clarity.
  • Prefer static_cast to C-style (double)sum.
  • == compares, = assigns.

Code examples

Integer and real division

C++
#include <iostream>
int main() {
int a = 17, b = 5;
std::cout << a / b << ' ' << a % b << ' ' << static_cast<double>(a) / b << '\n';
return 0;
}
Output
3 2 3.4

bool results

C++
#include <iostream>
int main() {
int x = 7;
std::cout << std::boolalpha << (x > 5) << ' ' << (x % 2 == 0) << ' ' << !(x == 7) << '\n';
return 0;
}
Output
true false false

More examples

Short-circuit and increments

C++
#include <iostream>
int calls = 0;
bool check(bool v) { ++calls; return v; }
int main() {
bool r = check(false) && check(true);
std::cout << std::boolalpha << r << " after " << calls << " call(s)\n";
int i = 5;
int a = i++;
int b = ++i;
std::cout << a << ' ' << b << ' ' << i << '\n';
std::cout << (2 + 3 * 4 > 10 ? "big" : "small") << '\n';
return 0;
}
Output
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, then i is 6; ++i makes it 7 and yields 7.
  • 2 + 3 * 4 is 14, which is greater than 10.

Expected output

Running “Short-circuit and increments” prints exactly:

TextOutput
false after 1 call(s)
5 7 7
big

Practical applications

  • % drives circular buffers and digit extraction.
  • Short-circuit guards protect pointer dereferences.
  • static_cast appears whenever integer averages must be computed exactly.

Best practices

  • Use named casts.
  • Parenthesise bitwise expressions used in conditions.
  • Prefer pre-increment ++i in loops (it never makes an unnecessary copy for iterators).

Common mistakes

Casting after dividing

Avoid

double avg = static_cast<double>(sum / n);

Prefer

double avg = static_cast<double>(sum) / n;

The division happens in integers before the cast.

Modifying a variable twice

Avoid

i = i++ + 1;

Prefer

i = i + 2;

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, but f(i++, i++) argument order is still unspecified.

Key takeaways

  • Integer / truncates; cast one operand for real division.
  • Logical operators yield bool and short-circuit.
  • Use static_cast rather than C-style casts.

Try it yourself

  1. 1
    Read seconds and print hours, minutes and seconds using / and %.
  2. 2
    Predict 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?
Practise this topic