{ "metadata": { "name": "while-loops" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "While Loops\n", "===========\n", "\n", "Basics\n", "------\n", "One of the most powerful aspects of computer programs is their ability to\n", "perform repetion, which is the ability to do something many times.\n", "The simplest form of repetition is the *while loop*, which does something\n", "as long as some condition is true.\n", "\n", "For example, here is a small program that prints out the numbers 3, 2, and 1 using a while loop." ] }, { "cell_type": "code", "collapsed": false, "input": [ "num_moons = 3\n", "while num_moons > 0:\n", " print num_moons\n", " num_moons = num_moons - 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3\n", "2\n", "1\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The loop opens with the keyword `while`, followed by the loop\u2019s controlling test.\n", "Since this test is true, i.e., num_moons is greater than 0 to start, Python\n", "executes the statements in the loop body, which is the set of indented statements\n", "under the loop control. This prints out `3` and subtracts 1 from `num_moons`.\n", "Python then re-checks the condition. It\u2019s still true so the program prints \u20192\u2032 and\n", "subtracts 1 from num_moons again. Another check, another print statement:\n", "the program prints \u20191\u2032, then decrements num_moons again.\n", "Since the loop\u2019s controlling condition is now false, the program is done.\n", "\n", "We can see that this is the case by looking at the current value of num_moons." ] }, { "cell_type": "code", "collapsed": false, "input": [ "num_moons" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 2, "text": [ "0" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "While loops don't always execute\n", "--------------------------------\n", "A while loop may execute any number of times, including zero. If instead of starting\n", "at 3, `num_moons` starts at -3, the loop condition is false the first time it is tested\n", "and the loop body doesn't execute at all." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 'start'\n", "num_moons = -3\n", "while num_moons > 0:\n", " print num_moons\n", " num_moons = num_moons - 1\n", "print 'end'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "start\n", "end\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see that the code has been run, because it prints out the words 'start' and 'end',\n", "but nothing else is printed because the loop did not execute.\n", "It is important to keep this \u201czero pass\u201d condition in mind when designing and testing code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Infinite loops\n", "--------------\n", "A while loop may also execute indefinitely.\n", "Here\u2019s another copy of the program that doesn\u2019t subtract 1 from num_moons inside the loop body." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 'start'\n", "num_moons = 3\n", "while num_moons > 0:\n", " print num_moons\n", "print 'end'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we executed this program (not recommended) it would first the word 'start',\n", "then it would print out the number 3, then another 3, and it would keep printing 3's\n", "until it was interupted.\n", "Since the loop\u2019s control condition was true when the loop started, and nothing happens\n", "inside the loop to change it, the loop will run until the user gets bored enough to halt it.\n", "This is usually not the desired behavior, but there are cases where an apparently infinite loop\n", "can be useful." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Video presentation\n", "------------------\n", "The first half of this video covers while loops using the examples above.\n", "\n", "" ] } ], "metadata": {} } ] }