Procedural Programming with C++
Cheat sheet
C++ Loops on One Card: Recall Before It Fills
Loops on one card: can you fill each line before I say it?
Quick check · tap an answer to continue
Lesson notes
Choose the right loop, and trace loop counters to predict exact iteration counts
Key points
- for loop: Use for when you know the count, like printing 1 to 10.
- while loop: While checks its loop condition first, so the body may run zero times.
- do-while loop: Do-while checks after the body, so it always runs at least once.
- Sentinel -1: A sentinel like -1 stops a sum loop when the user enters it.
- < vs <= (off-by-one): i < 10 from zero runs ten times; i <= 10 runs eleven.
- Trace table: Record each variable per iteration to predict exact output in exams.
Takeaway: Known count: for. Maybe zero: while. At least once: do-while.
Quick check: Which loop runs its body once even when its condition is false at the start?
- while
- do-while
- for
Do-while tests the condition only after executing the body once.
loop conditionsentineloff-by-one error
Lessons like this, for your syllabus.
Short narrated study videos written for your course, level and exam. Free to start.
Get lessons for your own syllabus