Learning outcomes
By the end of this lecture, you should be able to:- distinguish
break,continue, andpass - predict output in loop-control MCQs
- avoid infinite loops with
continue
break
- exits the nearest loop immediately
1 2 3 4
continue
- skips current iteration remainder
- moves to next iteration
1 3 5 7
pass
- does nothing (placeholder)
- keeps syntax block valid
0 1 2
Side-by-side meaning
| Keyword | Effect |
|---|---|
break | terminate loop |
continue | skip to next iteration |
pass | no operation |
Nested loop nuance with break
break affects inner loop only.
Infinite-loop trap with while + continue
i == 2 forever because update is skipped.
Safe version:
Exam hints and traps
passdoes not skip loop likecontinuebreakexits loop; code after loop still runs- in nested loops, identify which loop each statement controls
Practice
- Print numbers 1 to 20, stop at first multiple of 7.
- Print all numbers 1 to 10 except 4 and 8.
- Use
passinside unfinishedifblock example.
