C++ Control Flow Quiz: If, Switch and Loops Practice Test
Test your C++ control flow with 14 questions and three programs on if, switch, fallthrough, for and while loops, range-based for, EOF reading and two pointers.
- Course: C++ study plan
- Module: Control flow
- Kind: Checkpoint — cleared at 70%
- Reading time: 25 min
- Runtime: Clang 18 · C++20
Checkpoint — Control flow is the checkpoint that closes the Control flow module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers the whole module: branching and the conditional operator, switch with fallthrough and enums, the three loops and the unsigned countdown, range-based for with auto& and structured bindings, and the loop patterns from search to two pointers.
How it works. Fourteen questions and three programs. You need 70% on the questions and every program accepted to clear the module. You can retake it as often as you like; your best score counts.
Before you start, make sure you can answer these from memory:
- Why is
if (0 < x < 10)true for everyx? - What does
[[fallthrough]];change at run time, and what does it change at compile time? - Why does
for (std::size_t i = v.size() - 1; i >= 0; --i)never terminate? - What is the difference between
for (auto s : v)andfor (auto& s : v)? - Why is
while (!std::cin.eof())one iteration too many? - When is a pair of indices moving toward each other better than two nested loops?
The three programs are a run-length encoder that reads lines with std::getline after a count, a bank ledger that reads commands until the input ends, and a prime lister built from nested loops with an early exit. Read the input format carefully before writing the loop.
Common questions
What does [[fallthrough]]; change at run time and at compile time?
At run time, nothing: it is an empty statement. At compile time it records that a case body is meant to continue into the next one and stops -Wimplicit-fallthrough warning about it, which a comment cannot do.
When are two indices moving toward each other better than two nested loops?
When the data is sorted. Each comparison then rules out one index for good — if the sum is too small, the low element cannot pair with anything at or below the high index — so one pass of at most n - 1 steps replaces about n²/2 pair checks.
Why is while (!std::cin.eof()) one iteration too many?
eof() turns true only after a read has tried to go past the end of the input. After the last value is read it is still false, so the loop runs once more, the read fails, and the body processes whatever the failed read left behind.
Exercises
Run-length encoding
Read an integer n, then n lines of text (each at least one character; a line may contain spaces). For each line print its run-length encoding: every maximal run of one repeated character becomes the character followed by the run's length.
Read n with std::cin >>, then call std::cin.ignore() before the first std::getline — the newline after n would otherwise be read as an empty line. Walk each line with two indices: i marks the start of a run and j advances while the character repeats. std::to_string(k) turns a count into text.
Input: n, then n lines. Output: n lines.
Example: 2 / aaabccdddd / abc →
a3b1c2d4
a1b1c1A bank ledger
Read commands until the end of input — while (std::cin >> word) — and keep a balance in a long long that starts at 0:
deposit N— addNwithdraw N— subtractN, unlessNexceeds the balance, in which case printinsufficient fundsand leave itbalance— printbalance=<value>- any other word — print
unknown: <word>
Map each word to an enum class and switch on it, handling every enumerator without a default. After the input ends print final=<balance> deposits=<number of deposits> rejected=<number of refused withdrawals>.
Input: commands, one per line, until end of input (possibly none). Output: one line per balance, refusal and unknown word, then the final line.
deposit 100
withdraw 30
balance
withdraw 500
balance
prints
balance=70
insufficient funds
balance=70
final=70 deposits=1 rejected=1Primes up to n
Read an integer n (1 ≤ n ≤ 10 000) and print every prime number from 2 to n on one line separated by single spaces — or none when there is no prime in range — followed by a line count=<how many>.
Test each candidate by trial division in a nested loop: divisors d from 2 while d * d <= candidate; break at the first divisor and continue the outer loop for a composite. Build the first line in a std::string and print it once, so no trailing space is produced.
Input: n. Output: two lines.
Example: 20 →
2 3 5 7 11 13 17 19
count=8In this module: Control flow
- Branching — if, else and the conditional operator
- switch — labels, fallthrough and what to switch on
- Loops — for, while, do…while and the traps between them
- Range-based for — iterating without indices
- Loop patterns — search, accumulate, sentinel, EOF and two pointers
- Checkpoint — Control flow (this lesson)
← Loop patterns — search, accumulate, sentinel, EOF and two pointers · Declarations and definitions — the shape of a function →