Paul's Python CoursePPC

while loops

Not met yet

A while loop repeats its body for as long as its test is True. The test is asked at the top, before every pass, including the first: if it starts out False, the body never runs. The test is not watched during the body, only asked again at the top. Use while when you don't know in advance how many passes there will be.

Try it

main.py

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks a while loop runs its body once, like an if.
  • Thinks a while loop stops the moment its test becomes False, even halfway through the body.
  • Thinks the body of a while loop always runs at least once, even if the test starts out False.

Where it comes up