)` and execute the indented block of code if `True` is returned. Otherwise, "exit" the while-loop, skipping past the indented code.
- If the indented block code is executed, go back to the first step.
To be concrete, let's consider the example:
```python
# demonstrating a basic while-loop
total = 0
while total < 2:
total += 1 # equivalent to: `total = total + 1`
print(total) # `total` has the value 2
```
This code will perform the following steps:
1. Define the variable `total`, and assign it the value `0`
2. Evaluate `0 < 2`, which returns `True`: enter the enclosed code-block
3. Execute the code block: assign `total` the value `0 + 1`
4. Evaluate `1 < 2`, which returns `True`: enter the enclosed code-block
5. Execute the code block: assign `total` the value `1 + 1`
6. Evaluate `2 < 2`, which returns `False`: *skip* the enclosed code-block
7. Print the value of `total` (2)
Note that if we started off with `total = 3`, the condition-expression `3 < 2` would evaluate to `False` outright, and the indented body of code would never be reached.
**Warning!**
It is possible to write a while-loop such that its conditional statement is always True, in which case your code will run ceaselessly! If this ever happens to you in a Jupyter notebook, either interrupt or restart your kernel.
**Reading Comprehension: A basic while-loop**
Given a list of nonzero, positive numbers, `x`, append the sum of that list to the end of it. Do this until the last value in `x` is at least 100. Use a while-loop.
If you start with `x = [1]`, then by the end of your while-loop `x` should be `[1, 1, 2, 4, 8, 16, 32, 64, 128]`.
## `break`, `continue`, & `else` clauses on loops
The `continue` and `break` statements can be used within the bodies of both for-loops and while-loops. They provide added means for "short-circuiting" or prematurely exiting a given loop, respectively.
Encountering `break` within a given loop causes that loop to be exited immediately:
```ipython
# breaking out of a loop early
>>> for item in [1, 2, 3, 4, 5]:
... if item == 3:
... print(item, " ...break!")
... break
... print(item, " ...next iteration")
```
```
1 ...next iteration
2 ...next iteration
3 ...break!
```
An `else` clause can be added to the end of any loop. The body of this else-statement will be executed *only if the loop was not exited via a `break` statement*.
```ipython
# including an else-clause at the end of the loop
>>> for item in [2, 4, 6]:
... if item == 3:
... print(item, " ...break!")
... break
... print(item, " ...next iteration")
... else:
... print("if you are reading this, then the loop completed without a 'break'")
```
```
2 ...next iteration
4 ...next iteration
6 ...next iteration
if you are reading this, then the loop completed without a 'break'
```
The `continue` statement, when encountered within a loop, causes the loop-statement to be revisited immediately.
```python
# demonstrating a `continue` statement in a loop
>>> x = 1
>>> while x < 4:
... print("x = ", x, ">> enter loop-body <<")
... if x == 2:
... print("x = ", x, " continue...back to the top of the loop!")
... x += 1
... continue
... x += 1
... print("--reached end of loop-body--")
```
```
x = 1 >> enter loop-body <<
--reached end of loop-body--
x = 2 >> enter loop-body <<
x = 2 continue...back to the top of the loop!
x = 3 >> enter loop-body <<
--reached end of loop-body--
```
**Reading Comprehension: conducting flow in a loop**
Loop over a list of integers repeatedly, summing up all of its even values, and adding the content to a total. Repeat this process until the the total exceeds 100, or if you have looped over the list more than 50 times. Print the total only if it exceeds 100.
## Links to Official Documentation
- ['for' statement](https://docs.python.org/3/reference/compound_stmts.html#the-for-statement)
- ['while' statement](https://docs.python.org/3/reference/compound_stmts.html#the-while-statement)
- ['break', 'continue', and 'else' clauses](https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops)
- ['pass' statment](https://docs.python.org/3/tutorial/controlflow.html#pass-statements)
## Reading Comprehension Exercise Solutions:
**A basic for-loop: Solution**
```python
for letter in "abcdefghij":
if letter in "aeiou":
print(letter)
```
**A basic while-loop: Solution**
```python
while x[-1] < 100:
x.append(sum(x))
```
**Conducting flow in a loop: Solution**
```python
x = [3, 4, 1, 2, 8, 10, -3, 0]
num_loop = 0
total = 0
while total < 100:
for item in x:
# return to for-loop if
# `item` is odd-valued
if item % 2 == 1:
continue
else:
total += item
num_loop += 1
# break from while-loop if
# more than 50 items tallied
if 50 < num_loop:
break
else:
print(total)
```