)` and execute the indented block of code if `True` is returned. Otherwise, \"exit\" the while-loop, skipping past the indented code.\n",
"- If the indented block code is executed, go back to the first step.\n",
"\n",
"To be concrete, let's consider the example:\n",
"```python\n",
"# demonstrating a basic while-loop\n",
"total = 0\n",
"while total < 2:\n",
" total += 1 # equivalent to: `total = total + 1`\n",
"\n",
"print(total) # `total` has the value 2\n",
"```\n",
"\n",
"This code will perform the following steps:\n",
"\n",
"1. Define the variable `total`, and assign it the value `0`\n",
"2. Evaluate `0 < 2`, which returns `True`: enter the enclosed code-block\n",
"3. Execute the code block: assign `total` the value `0 + 1`\n",
"4. Evaluate `1 < 2`, which returns `True`: enter the enclosed code-block\n",
"5. Execute the code block: assign `total` the value `1 + 1`\n",
"6. Evaluate `2 < 2`, which returns `False`: *skip* the enclosed code-block\n",
"7. Print the value of `total` (2)\n",
"\n",
"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.\n",
"\n",
"\n",
"\n",
"**Warning!** \n",
"\n",
"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.\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"**Reading Comprehension: A basic while-loop**\n",
"\n",
"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. \n",
"\n",
"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]`.\n",
"\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `break`, `continue`, & `else` clauses on loops\n",
"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.\n",
"\n",
"Encountering `break` within a given loop causes that loop to be exited immediately:\n",
"\n",
"```ipython\n",
"# breaking out of a loop early\n",
">>> for item in [1, 2, 3, 4, 5]:\n",
"... if item == 3:\n",
"... print(item, \" ...break!\")\n",
"... break\n",
"... print(item, \" ...next iteration\")\n",
"```\n",
"```\n",
"1 ...next iteration\n",
"2 ...next iteration\n",
"3 ...break!\n",
"```\n",
"\n",
"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*.\n",
"\n",
"```ipython\n",
"# including an else-clause at the end of the loop\n",
">>> for item in [2, 4, 6]:\n",
"... if item == 3:\n",
"... print(item, \" ...break!\")\n",
"... break\n",
"... print(item, \" ...next iteration\")\n",
"... else:\n",
"... print(\"if you are reading this, then the loop completed without a 'break'\")\n",
"```\n",
"```\n",
"2 ...next iteration\n",
"4 ...next iteration\n",
"6 ...next iteration\n",
"if you are reading this, then the loop completed without a 'break'\n",
"```\n",
"\n",
"The `continue` statement, when encountered within a loop, causes the loop-statement to be revisited immediately.\n",
"```python\n",
"# demonstrating a `continue` statement in a loop\n",
">>> x = 1\n",
">>> while x < 4:\n",
"... print(\"x = \", x, \">> enter loop-body <<\")\n",
"... if x == 2:\n",
"... print(\"x = \", x, \" continue...back to the top of the loop!\")\n",
"... x += 1\n",
"... continue\n",
"... x += 1\n",
"... print(\"--reached end of loop-body--\")\n",
"```\n",
"```\n",
"x = 1 >> enter loop-body <<\n",
"--reached end of loop-body--\n",
"x = 2 >> enter loop-body <<\n",
"x = 2 continue...back to the top of the loop!\n",
"x = 3 >> enter loop-body <<\n",
"--reached end of loop-body--\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"**Reading Comprehension: conducting flow in a loop**\n",
"\n",
"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.\n",
"\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Links to Official Documentation\n",
"\n",
"- ['for' statement](https://docs.python.org/3/reference/compound_stmts.html#the-for-statement)\n",
"- ['while' statement](https://docs.python.org/3/reference/compound_stmts.html#the-while-statement)\n",
"- ['break', 'continue', and 'else' clauses](https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops)\n",
"- ['pass' statment](https://docs.python.org/3/tutorial/controlflow.html#pass-statements)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reading Comprehension Exercise Solutions:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**A basic for-loop: Solution**\n",
"```python\n",
"for letter in \"abcdefghij\":\n",
" if letter in \"aeiou\":\n",
" print(letter)\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**A basic while-loop: Solution**\n",
"```python\n",
"while x[-1] < 100:\n",
" x.append(sum(x))\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Conducting flow in a loop: Solution**\n",
"\n",
"```python\n",
"x = [3, 4, 1, 2, 8, 10, -3, 0]\n",
"num_loop = 0\n",
"total = 0\n",
"\n",
"while total < 100:\n",
" for item in x:\n",
" # return to for-loop if \n",
" # `item` is odd-valued\n",
" if item % 2 == 1:\n",
" continue\n",
" else:\n",
" total += item\n",
" num_loop += 1\n",
" \n",
" # break from while-loop if \n",
" # more than 50 items tallied\n",
" if 50 < num_loop:\n",
" break\n",
"else:\n",
" print(total)\n",
"```\n"
]
}
],
"metadata": {
"jupytext": {
"text_representation": {
"extension": ".md",
"format_name": "markdown",
"format_version": "1.2",
"jupytext_version": "1.3.0rc1"
}
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}