Iteration — while, do-while, for, and Nested Loops

← Course home · ← Decisions module · Unit 05–06/16 · Syllabus

Welcome to the engine room of programming. Units 04–05 taught your programs to choose; this module teaches them to repeat. With these four lessons, Stage B’s toolkit is complete — everything after (functions, arrays, files, objects) organizes or scales what you build here.

In this module you will learn

Module map

Page What’s inside
Lesson 1 — The while loop and the accumulator Loop anatomy, counters vs accumulators, infinite loops, do-while, input-controlled loops, sentinel preview
Lesson 2 — for, idioms, and choosing for anatomy, the classic idioms (sum/avg/min/max), off-by-one boundaries, choosing the appropriate loop
Lesson 3 — break, continue, sentinels The two exits and the shortcut; sentinel loops; menus; a validation-loop gallery; pattern: prime test
Lesson 4 — Nested loops, digits, patterns Nested-loop execution model, tables/patterns, digit processing, primes, factorial, common loop errors gallery
Exercises 32 exercises with separated solutions (S1–S32)
Predictions 15 predict-the-output problems, answers in a separated answer section
Debugging 15 loop bugs — find/fix/reflection format
Challenges 15 challenges with separated solutions
Labs 10 lab scenarios with full dry-run tables
Mini-project The Number Analysis Toolkit — a loop-based menu program

Try it yourself first

Every problem page states the rule once: attempt for 15 minutes before opening any solution. Attempting first isn’t just honesty training — it makes the solution readable (you know which step you were missing), and retrieval practice is what turns reading into skill. The solutions in this module are deliberately written to be complete and separated, so a stuck student gets a full path — but the order you meet them in is always: problem → attempt → solution.

Each exercise page lists difficulty (★ = first pass, ★★ = needs the idioms, ★★★ = combines ideas), and the hub’s pacing table maps pages to sessions.

Pacing

Units 05–06 span two weeks (4 sessions). A workable split for self-study:

What comes next

After the mini-project, Stage C begins: the functions module turns your idioms into named, reusable machines — the accumulator-idiom programs from Lesson 2 will all become three-line programs with a loop in a function.

Checklist