Introduction: Being selective đ§
Module 3 focuses on "selection structures". Jargon I know, but the key to focus on is "select". Most computation procedures are not an unvarying sequence of steps to carry out like the SIPO programs in Module 2, instead they have to select which operations to carry out depending on circumstances.
They might have to alter their output messages depending on the result of a calculation, e.g. if your % mark is high you get an A, but if it is too low a D!
Or they might alter the calculations they carry out depending on an intermediate result, e.g. they might charge regular price for small orders, but give a discount if an order is large enough.
Or they might even alter what task they undertake in the first place depending on the user's actions, e.g. in desktop software what action to take depends on which menu item the user selects.
In all these cases what we need is a way to do one thing or another (but not both). This week's resources will show you how to achieve that in Python.
One thing to watch for is how we code our selection structures efficiently, so they don't perform more tests than necessary. Efficiency wasn't much of an issue in SIPO programs, but it starts to become one here.
if:
 A Simple (artificial) Example- Correcting an Oversight
if
 Syntax, in Detail- Relational Expressions
- Compound Statements and Indentation
- Example: One program to convert F to C_or_C to F
- The Problem
- Version 1
- Version 2
- Testing Selection
- Performance Analysis*
- A Final (Very Small) Example: Absolute value
- Summary: Selection
usingÂ
if
- Gazinta (as in "2 Gazinta 8, but 3 doesn't")
- Ode to Pythagoras
- Utility bills
- Beaufort Scale
- Exercise sheet 2
- Assignment 3