while loops
The while loop is a standard workhorse of many languages. Essentially, the program will continue doing something while a certain condition exists. As soon as that condition is no longer true, the loop stops.
break and continue work exactly the same as in C. The equivalent of C's empty statement (a semicolon) is the pass statement, and Python includes an else statement for use with breaks.
The break statements simply force the loop to quit early; when used with nested loops, it only exits the innermost enclosing loop. The continue statements cause the loop to start over with the next iteration, regardless of any other statements further on in the loop. The else statement block is run "on the way out" of the loop, unless a break ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access