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.

  1. if: A Simple (artificial) Example
  2. Correcting an Oversight
  3. if Syntax, in Detail
  4. Relational Expressions
  5. Compound Statements and Indentation
  6. Example: One program to convert F to C_or_C to F
  7. The Problem
  8. Version 1
  9. Version 2
  10. Testing Selection
  11. Performance Analysis*
  12. A Final (Very Small) Example: Absolute value
  13. Summary: Selection using if
  14. Gazinta (as in "2 Gazinta 8, but 3 doesn't")
  15. Ode to Pythagoras
  16. Utility bills
  17. Beaufort Scale
  18. Exercise sheet 2
  19. Assignment 3