Step a for loop one part at a time, then take the i++ away and watch the check never change its mind. The third shape is still where the bugs live.
for loop runs whenfor and while without thinking about itRepetition was the shape where bugs live, and that has not changed. C++ gives you three ways to write it, and they are the same idea underneath.
Use for when you are counting, while when you are waiting. That single sentence covers almost every loop you will write this year.
Watch which part fires next. The setup goes once and never again — everything after that is check, body, step, forever.
Press step. Then take the i++ away and press it again.
You will write an infinite loop this semester. When Terminal fills with output and will not stop, press Control + C. Not Command — Control. That interrupts the running program and gives you your prompt back. It is not a crash and you have broken nothing.
The most common loop bug in existence is not exotic. It is < where you meant <=, or the other way round.
Same loop, one character apart. Verified against real output.
Counting from 0 and stopping before the total is the C++ habit, and it is not arbitrary — it is exactly how lists are numbered, which you meet in the next level. Learn the shape i = 0; i < n; i++ as one unit and you will use it thousands of times.
Trace it — level 4, and it works exactly the same on real code. Write a column for i, one row per lap, and check the first and last rows. Loops are almost always right in the middle and wrong at the ends, so those are the only two rows worth your time.
Two words let you interrupt a loop mid-lap. They look similar and they do very different things — verified output below.
break means stop. continue means skip this one. Use break when you have found what you were looking for and there is no point going on — searching a list is the obvious case. Use continue to ignore items that do not qualify without nesting everything inside an if.
continue in a while loop skips everything below it — including the line that changes your counter. That is an infinite loop with a very innocent-looking cause, and it is why continue is safer in a for, where the step is part of the loop itself and always runs.
Both also apply only to the innermost loop they are inside. In a loop within a loop, break leaves the inner one and the outer one carries straight on — which surprises people every single time.
A while loop checks first and might never run at all. A do-while runs the body first and checks afterwards, so it always happens at least once. Verified with a value that fails the test from the start:
Note the semicolon after the closing while — it is required there and nowhere else, and forgetting it produces a confusing error.
Asking someone for input. You cannot check whether their answer is valid until you have asked for it once, so ask, then check, then ask again if needed is the natural shape:
do { cout << "Age: "; cin >> age; } while (age < 0);
That is nearly the only time it beats a plain while — which is why you will see it far less often than the other two.
A variable declared in the setup exists only inside the loop. After the closing brace it is gone, and asking for it will not compile:
That is deliberate and it is a kindness. If you genuinely need the value afterwards — how many did we get through before it stopped? — declare it before the loop instead. This is called scope, and it is the same idea that makes functions safe in the next level.
Say a loop out loud: “start at 1, keep going while it is under 5, add 1 each time.” Then perform it, saying every number.
Predict how many numbers will be said before A starts. Not which numbers — how many. Then count on your fingers.
Try the four combinations: start at 0 or 1, stop with < or <=. Only one of the four gives exactly five laps starting from zero, and getting that wrong by one is what an off-by-one error is. Then have A deliberately forget the “add 1” and let it run for ten seconds — feeling it is worth more than reading about it.
Known number of laps takes a for. Waiting for something to change takes a while.
The middle is always fine. Every off-by-one lives at one of the two ends.
Stops a runaway program. You will need it, and it breaks nothing.
Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.
Have a real go before revealing. Being wrong here is worth more than being right in three weeks.
What does this print?
How many times does this loop run?
What does this print?
n is 99, which is not less than 5. What does this print?
You write continue inside a while loop, above the line that increases the counter. What happens?
← back to the whole C++ track · stuck on anything? ask peter.