Repetition I — Loops
"Train yourself for godliness." — 1 Timothy 4:7
Why This Matters
This is the chapter the sport metaphor was built for. Up to now you've written programs that compute a thing once. Computers, the interesting parts of computers, do things over and over — billions of times a second.
Christian practice — Scripture reading, prayer, memorization, the daily office — is built on the same reps and discipline as any other skill. A Bible-reading plan is a loop. A prayer practice is a loop. The instinct of modern culture is to mistrust repetition as "rote." That instinct is wrong. Repetition is how anything that lasts gets built.
Three Loops, One Idea
while (condition) { ... }— keep running as long as condition is true. Check first.do { ... } while (condition);— run once, then keep running while true. Check after.for (init; condition; update) { ... }— structured loop with counter built in.
All three can express any loop. Each naturally fits certain situations. Pick the right shape for the job and the code reads itself.
Off-By-One
The single most famous beginner loop bug:
for (int i = 0; i <= 10; i++) {
cout << i << " ";
}
How many iterations? Eleven, not ten. i takes values 0 through 10 — that's 11
numbers. The fix: i < 10 for "10 iterations starting at 0," or
i <= 10 for "starting at 1."
Two idioms to memorize:
- Counting from 0:
for (int i = 0; i < n; i++)— runsntimes. - Counting from 1:
for (int i = 1; i <= n; i++)— runsntimes.
Infinite Loops
Sooner or later — probably this week — you will write a loop that never exits. The program will appear to hang. Press Ctrl+C in the terminal to kill it.
Almost every infinite loop is one of three bugs: the condition never becomes false, you
incremented the wrong variable, or your break never fires.
Coach's Note — Print the loop variable inside the body when confused. It makes the bug obvious instantly. Add the
coutwhile debugging; remove it once the loop works.
Accumulators and Counters
Two patterns you'll write over and over:
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
} sum starts at 0. Each iteration adds i. After the loop,
sum holds the total. The same pattern handles products (start at 1, multiply),
max-so-far, streaks — any "running result."
This Week's Project
You're ready for Project 4: Daily Practice Tracker. You'll build a program that tracks a real practice — Scripture reading, prayer, memorization, physical training, anything — across days. Bar-chart visualization, totals, averages, best and worst days. The chapter that proves discipline is data.
Loops — Quick Check
for (int i = 0; i <= 10; i++) {
cout << i << " ";
} i <= 10, so i takes values 0, 1, 2, ..., 10 — that's 11 values. This is the classic off-by-one bug. If you wanted 10 iterations from 0, use i < 10. sum?int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += i;
} i = 1, 2, 3, 4, 5. After each iteration sum becomes 1, 3, 6, 10, 15. That's the closed-form n(n+1)/2 = 5·6/2 = 15. int i = 1;
while (i <= 10) {
cout << i << " ";
} i, so the condition i <= 10 stays true forever. The loop prints "1 1 1 1..." until you kill the program. Add i++; inside the body. while (or while(true) with a break on -1) is the canonical fit. You don't know up front how many iterations there are — only the condition that should stop them.