3. Control Flow: Loops
While Loops
The
Here, expression is any valid Python expression that can be evaluated in boolean context, in other words, expressions that can be interpreted as either or . The indented statement refers to the body of the loop, the code that will be executed every iteration.
For example, here we see a simple loop that counts to . Naturally, values within the expression are also changed within the loop, if this wasn't the case the loop would run infinitely. |
|
You have probably noticed that is a very common variable assignment pattern that occurs in loops. It is in fact so common that this particular syntax received its own shorthand notation.
Shorthand | Expression |
The shorthand notation is supported by all arithmetic operators in Python.
>>> var = 0
>>> var += 1 >>> print(var) |
1
|
Or visit omptest.org if jou are taking an OMPT exam.