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:
- boolean test - an expression that evaluates to
True
orFalse
True
block - a block of code that is executed if the test evaluates toTrue
False
block - a block of code that is executed if the test evaluates toFalse
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.
Image reproduced from Chapter 2: Introduction to Computation and Programming Using Python.