While loops tell the computer to do something while something else is true. For example, run the code below:
The code in the Trinket above has one variable container, called age
, that starts at 10. We print the value of age
and then increase it by 1. We keep going so long as the value of age
is less than 20.
The general form of a while loop is:
while (True/False expression):
one_or_more_python_statements
And one way to "read" while loops is:
while #True or False statement:
# do this stuff
If you ask to do something while False
, nothing will happen:
Like if statements, Things only happen when the while
condition is True.