{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# while Loops\n",
"\n",
"The while
statement in Python is one of most general ways to perform iteration. A while
statement will repeatedly execute a single statement or group of statements as long as the condition is true. The reason it is called a 'loop' is because the code statements are looped through over and over again until the condition is no longer met.\n",
"\n",
"The general format of a while loop is:\n",
"\n",
" while test:\n",
" code statements\n",
" else:\n",
" final code statements\n",
"\n",
"Let’s look at a few simple while
loops in action. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x is currently: 0\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 1\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 2\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 3\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 4\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 5\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 6\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 7\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 8\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 9\n",
" x is still less than 10, adding 1 to x\n"
]
}
],
"source": [
"x = 0\n",
"\n",
"while x < 10:\n",
" print('x is currently: ',x)\n",
" print(' x is still less than 10, adding 1 to x')\n",
" x+=1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice how many times the print statements occurred and how the while
loop kept going until the True condition was met, which occurred once x==10. It's important to note that once this occurred the code stopped. Let's see how we could add an else
statement:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x is currently: 0\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 1\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 2\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 3\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 4\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 5\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 6\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 7\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 8\n",
" x is still less than 10, adding 1 to x\n",
"x is currently: 9\n",
" x is still less than 10, adding 1 to x\n",
"All Done!\n"
]
}
],
"source": [
"x = 0\n",
"\n",
"while x < 10:\n",
" print('x is currently: ',x)\n",
" print(' x is still less than 10, adding 1 to x')\n",
" x+=1\n",
" \n",
"else:\n",
" print('All Done!')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# break, continue, pass\n",
"\n",
"We can use break
, continue
, and pass
statements in our loops to add additional functionality for various cases. The three statements are defined by:\n",
"\n",
" break: Breaks out of the current closest enclosing loop.\n",
" continue: Goes to the top of the closest enclosing loop.\n",
" pass: Does nothing at all.\n",
" \n",
" \n",
"Thinking about break
and continue
statements, the general format of the while
loop looks like this:\n",
"\n",
" while test: \n",
" code statement\n",
" if test: \n",
" break\n",
" if test: \n",
" continue \n",
" else:\n",
"\n",
"break
and continue
statements can appear anywhere inside the loop’s body, but we will usually put them further nested in conjunction with an if
statement to perform an action based on some condition.\n",
"\n",
"Let's go ahead and look at some examples!"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x is currently: 0\n",
" x is still less than 10, adding 1 to x\n",
"continuing...\n",
"x is currently: 1\n",
" x is still less than 10, adding 1 to x\n",
"continuing...\n",
"x is currently: 2\n",
" x is still less than 10, adding 1 to x\n",
"x==3\n",
"x is currently: 3\n",
" x is still less than 10, adding 1 to x\n",
"continuing...\n",
"x is currently: 4\n",
" x is still less than 10, adding 1 to x\n",
"continuing...\n",
"x is currently: 5\n",
" x is still less than 10, adding 1 to x\n",
"continuing...\n",
"x is currently: 6\n",
" x is still less than 10, adding 1 to x\n",
"continuing...\n",
"x is currently: 7\n",
" x is still less than 10, adding 1 to x\n",
"continuing...\n",
"x is currently: 8\n",
" x is still less than 10, adding 1 to x\n",
"continuing...\n",
"x is currently: 9\n",
" x is still less than 10, adding 1 to x\n",
"continuing...\n"
]
}
],
"source": [
"x = 0\n",
"\n",
"while x < 10:\n",
" print('x is currently: ',x)\n",
" print(' x is still less than 10, adding 1 to x')\n",
" x+=1\n",
" if x==3:\n",
" print('x==3')\n",
" else:\n",
" print('continuing...')\n",
" continue"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note how we have a printed statement when x==3, and a continue being printed out as we continue through the outer while loop. Let's put in a break once x ==3 and see if the result makes sense:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x is currently: 0\n",
" x is still less than 10, adding 1 to x\n",
"continuing...\n",
"x is currently: 1\n",
" x is still less than 10, adding 1 to x\n",
"continuing...\n",
"x is currently: 2\n",
" x is still less than 10, adding 1 to x\n",
"Breaking because x==3\n"
]
}
],
"source": [
"x = 0\n",
"\n",
"while x < 10:\n",
" print('x is currently: ',x)\n",
" print(' x is still less than 10, adding 1 to x')\n",
" x+=1\n",
" if x==3:\n",
" print('Breaking because x==3')\n",
" break\n",
" else:\n",
" print('continuing...')\n",
" continue"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note how the other else
statement wasn't reached and continuing was never printed!\n",
"\n",
"After these brief but simple examples, you should feel comfortable using while
statements in your code.\n",
"\n",
"**A word of caution however! It is possible to create an infinitely running loop with while
statements. For example:**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# DO NOT RUN THIS CODE!!!! \n",
"while True:\n",
" print(\"I'm stuck in an infinite loop!\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"A quick note: If you *did* run the above cell, click on the Kernel menu above to restart the kernel!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}