June 2017
Beginner
352 pages
8h 39m
English
Many programming languages support a loop construct which places the predicate test at the end of the loop rather than at the beginning. For example, C, C++, C# and Java support the do-while construct. Other languages have repeat-until loops instead or as well. This is not the case in Python, where the idiom is to use while True together with an early exit, facilitated by the break statement.
The break statement jumps out of the loop — and only the innermost loop if severals loops have been nested — continuing execution immediately after the loop body.
Let's look at an example of break, introducing a few other Python features along the way, and examine it line-by-line:
>>> while True:... response = input()... if int(response) ...
Read now
Unlock full access