Lesson 7: for loops & nested loops
Last lesson we built a loop from three scattered parts: counter initialization, condition and advance. for (int i = 0; i < n; i++) packs all three into one line — init; condition; step — making it the natural choice when the number of rounds is known in advance. We'll also learn nested loops: a loop
for is like a recipe instruction: 'stir 10 times' — where you start, when you stop and what happens after each stir, all in one line. A nested loop is like a clock: for every single step of the hour hand, the minute hand completes a full lap.
- for (init; condition; step)
- A loop whose three parts sit in one header: for (int i = 0; i < n; i++).
- init; condition; step
- The init runs once; the condition is checked before each round; the step runs after each round.
- nested loop
- A loop inside a loop: the inner finishes all its rounds for each round of the outer.
- break
- Exits the (innermost) loop immediately, even if the condition is still true.
- continue
- Skips the rest of the current round and jumps straight to the step and the condition check.