A loop runs the same block again and again — with a counter or a condition deciding when to stop.
1 for
Know the count? for (int i = 1; i <= 4; i++) packs init, test and update in one line.
2 while
Repeat while a condition holds — the test runs first, the body after.
3 do-while
The body runs once before the first check — perfect for menus.
4 The update
i++ moves the loop toward its end. Forget it and the loop never stops.
5 Off by one
i < 4 runs 4 times; i <= 4 runs 5. One character, one extra pass.
Repeat without repeating yourself
for1Know the count? for (int i = 1; i <= 4; i++) packs init, test and update in one line.
while2Repeat while a condition holds — the test runs first, the body after.
do-while3The body runs once before the first check — perfect for menus. The update4i++ moves the loop toward its end. Forget it and the loop never stops. Off by one5i < 4 runs 4 times; i <= 4 runs 5. One character, one extra pass. 5 building blocks make up Repeat without repeating yourself
Three loops, one job each: counted repeats, check-first repeats, and run-at-least-once repeats.
process decision input / output start / end Press Run to see the four moments every for-loop pass goes through.
Press play — watch i climb from 1 to 4 while sum collects each value.
preprocessThe preprocessor pastes in iostream for cout.
Memory
no variables yet — press play
Console
output appears as the program runs…
A while loop counts down — where exactly does it stop?
Your predicted output — one line per console line
Hint: The body prints n and then decrements it. When n reaches 0 the condition fails — Go! prints after the loop.
01for = counted repeats, while = check-first repeats, do-while = runs at least once.
02Every loop needs three parts: a start, a test that can fail, and an update.
03The counter changes every pass — that is what eventually ends the loop.
04i < n is the safe habit for n repetitions; i <= n goes one past the end.