Unit 3 - Branching and Decisions

So far in this course, we have written straight-line programs. These execute one statement after another.

These aren't particularly interesting programs.

Branching programs provide more complexity to our programs and make them more interesting.

Generally, we will look to use a conditional statement which has three parts:

  1. boolean test - an expression that evaluates to True or False
  2. True block - a block of code that is executed if the test evaluates to True
  3. False block - a block of code that is executed if the test evaluates to False

 

The following image demonstrates the program flow of a basic branching statement.

You can see that the code enters the conditional statement (dotted box) and encounters the Test, based on whether it evaluates to True or False it executes either the True block or the False block.

Flow chart for conditional statement Image reproduced from Chapter 2: Introduction to Computation and Programming Using Python.