Published online by Cambridge University Press: 05 February 2015
Controlling command execution
On the whole, a program will normally run by executing the stated commands, one after the other in sequential order, like reading the lines of a book. Frequently, however, you will need the program to deviate from this, to jump to a line that is not the next line. There are three main ways of diverting from the line-by-line paradigm. The first way is through the use of functions (subroutines), where the program’s execution jumps from a particular line of code to an entirely different spot, even in a different file or module, to do a task before (usually) jumping back again. You can even jump to a function’s subroutine from inside another function, and do this repeatedly, so that there is a stack of jumps between the current line and the first jump point. Given their importance, functions will be discussed in a dedicated chapter that follows this one.
The second way of jumping between program lines is through the use of conditional statements. Here you can check if some statement or expression is true, and if it is then you continue on with the following block of code, otherwise you might skip it or execute a different bit of code. The third way is by performing repetitive loops through the same lines of code, where each time through the loop different values may be used for the variables. Usually such loops are either done a specific number of times or until something in particular happens. When you have a collection of items, like a list, a loop can be used to consider all of the items in turn; the loop iterates over the items of the collection. A significant number of people who are new to programming find this the hardest idea to get to grips with, although Python’s syntax makes it about as easy as it can be.
To save this book to your Kindle, first ensure [email protected] is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.
Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.
Find out more about the Kindle Personal Document Service.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.