Lesson · Control Flow
Control flow practice: ten programs worth writing
14 min readPractice
The exercise set that turns syntax into fluency: primes, Fibonacci, factorials, palindromes, digit sums, GCD, pattern printing and small games, each with the idea it drills.
How to use this page
Reading about loops does not build loop intuition; writing ten small programs does. Each problem below names the control-flow idea it drills and the trap it tends to expose. Solve them in jshell or a scratch file before reading the solution sketch, and judge yourself on whether the loop boundaries were right the first time.
Number problems
These build counting loops, early exits and integer arithmetic discipline:
- Sum of 1 to n: the canonical for loop. Check your off-by-one; the closed form n(n+1)/2 is your test oracle.
- Factorial of n: multiplication accumulation. Watch overflow above 20; that is a lesson in long and BigInteger, not a failure.
- Fibonacci first n: two-variable iteration (a, b = b, a + b). The naive recursive version is the recursion topic's cautionary tale.
- Prime test: trial division up to sqrt(n) with an early break. The first place break pays its rent; also the first place you meet the sqrt boundary.
- Digit sum: while (n > 0) { sum += n % 10; n /= 10; }. Modulo and integer division doing structural work.
- Reverse a number: the same peeling loop, building the reversed value with r = r * 10 + digit.
Palindromes and patterns
Two-pointer and nested-loop shapes live here:
- Palindrome check: two pointers walking toward the middle, exiting early on mismatch. The index conditions are the whole exercise.
- Star pyramid: nested loops where the inner loop's bound depends on the outer variable. Draw the output for n = 3 first: every pattern bug is a boundary bug.
- Multiplication table: a fixed grid; the exercise is formatting, and the formatting topic comes later with printf.
- GCD (Euclid): while (b != 0) { t = b; b = a % b; a = t; }. Four lines that demonstrate why modulo is more than a remainder operator.
Judging your own solutions
Three questions separate a working loop from a good one. Are the boundaries half-open and consistent? Does it work for the empty and single-element cases (n = 0, n = 1)? Does it exit early when it can: break on found, return from a search method, skip with continue instead of nesting deeper?
When a solution compiles but misbehaves, print the loop variable on every round. The output shows the boundary bug in one glance, and this habit scales to every algorithm later in the track, where the same technique is called a step trace.
Overflow arrives earlier than pride expects
Factorial is an int-sized lie past 12 and a long-sized lie past 20. Fibonacci overflows even sooner. The number problems are the natural first meeting with silent overflow; check results against a BigInteger or a table, and treat the mismatch as the exercise teaching you, not the code failing you.
Interview angles
Why do small exercises matter for control flow?+
They are the only place boundaries are cheap to get wrong. Ten programs of ten lines each surface every off-by-one and early-exit mistake in a context where they take seconds to debug, which is the point before real algorithms arrive.
How do you test a solution to the sum of 1 to n?+
Against the closed form n(n + 1) / 2 for a handful of values, plus the edge cases 0 and 1. Using an independent formula as an oracle is the same muscle as property-based testing later.
What does the GCD loop demonstrate?+
Rewrite-based iteration: the state pair shrinks every round with no explicit counter, and termination is a property of the values. It is the first loop whose exit is proved rather than eyeballed.