Chapter 04 · Week 4

Repetition I — Loops

"Train yourself for godliness." — 1 Timothy 4:7
4

Watch a loop actually run

Loops update invisible state on every iteration. Most beginner bugs come from misunderstanding what changes when. Step through one below before you read.

Loop Tracer

Loops update invisible state on every iteration. Press play below to watch each step happen one at a time, or use step to advance manually. The variables on the right update exactly when the code shown on the left runs.

Preset:
Code
 
State
Output so far:
 
Iteration 0 of 0
Watch the off-by-one: in "Count 1→N", the condition is i <= N so the loop runs N times. In "Sum 1→N" the same. If you accidentally write i < N instead, you get one fewer iteration — the off-by-one bug. Try changing it in your head as the tracer runs.

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++) — runs n times.
  • Counting from 1: for (int i = 1; i <= n; i++) — runs n times.

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 cout while 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.

Check Your Reps

Loops — Quick Check

Question 1 of 4
How many times does this loop run?
for (int i = 0; i <= 10; i++) {
    cout << i << " ";
}
Why: The condition is 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.
Question 2 of 4
What's the final value of sum?
int sum = 0;
for (int i = 1; i <= 5; i++) {
    sum += i;
}
Why: The loop runs 5 times with 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.
Question 3 of 4
What's wrong with this loop?
int i = 1;
while (i <= 10) {
    cout << i << " ";
}
Why: Nothing inside the loop changes 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.
Question 4 of 4
Which loop best fits "ask the user for numbers until they type -1"?
Why: A sentinel-controlled 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.
YOU FINISHED. NICE WORK.

← WEEK 3: CONDITIONALS   ·   WEEK 5: FUNCTIONS →