while loops, break/continue, sentinels and EOF
- Stage
- 1
- Module
- M1.6
- Lessons
- 1
In this module
- Repeat a block for as long as a condition holds, with
while. - Read values until a sentinel value appears, ending the loop when it does.
- Exit a loop early with
break, and skip straight to the next pass withcontinue. - Recognize the
EOFErrora loop can raise reading past the end of input, and read every remaining line safely withimport sys.
Before this module
Lessons
Practice
Try these on the judges. Each link opens the problem on WMOJ or DMOJ.
- 2024 J2Dusa And The Yobis (opens on WMOJ in a new tab) WMOJ
Grow a creature by feeding it smaller creatures, one at a time, until it meets one it cannot eat.
- 2021 J3Secret Instructions (opens on WMOJ in a new tab) WMOJ
Follow a sequence of instruction codes until a stop code appears.
- 2020 J2Epidemiology (opens on DMOJ in a new tab) DMOJ
Simulate an outbreak day by day until the total infected passes a limit.
Why DMOJ: Tries the same kind of problem on DMOJ, which holds 2014 to 2020.