CP104: Notes - Fallthrough Algorithms

Conditions in an if-elif-else decision structure are mutually exclusive. That is, the first condition that is true triggers the execution of the statements it owns (i.e. are indented within it), and all other conditions and nested statements are ignored. Even if following conditions are true, their statements are not executed. This allows us to write fallthrough algorithms that take advantage of the fact that only one part of a decision structure is executed.

The following code decides what sort of member a user is depending on how many reward points they've gathered:

    
# Constants
SILVER = 10000
GOLD = 25000
PLATINUM = 50000

print('This program determines your status as a member of our reward program.')
points = int(input('Enter your total reward points: '))

if points < SILVER:
    # If the member has less than 10,000 points: ordinary member.
    print('You are an Ordinary member.')
elif points >= SILVER and points < GOLD:
    # If the member has between 10,000 and 25,000 points: silver member.
    print('You are a Silver member.')
elif points >= GOLD and points < PLATINUM:
    # If the member has more than 25,000 points and up to 50,000 points, they
    # are a gold member.
    print('You are a Gold member.')
elif points >= PLATINUM:
    # If the member has more than 50,000 points they are a PLATINUM member.
    print('You are a Platinum member.')

  

Although this code works just fine, it is unnecessarily complex. The boolean operator and is not needed. Suppose a member has 5,000 points - they not only have less than 10,000 points, they also certainly have less than 25,000 and less than 50,000 points. Once it is determined that they have less than a certain amount of points, there is no need to check that condition a second time. The >= checks are not necessary - those execution paths would not be reached if the number of points was too small. The previous if structure could be replaced by:

    
if points < SILVER:
    print('You are an Ordinary member.')
elif points < GOLD:
    print('You are a Silver member.')
elif points < PLATINUM:
    print('You are a Gold member.')
else:
    print('You are a Platinum member.')

  

In this code the execution path falls through all conditions after the first one that is matched. There is no need for a second condition check for conditions that are never evaluated. All these conditions are implicitly, rather than explicitly, mutually exclusive.

Again, suppose a member has 5,000 points. Only the first of these mutually exclusive if-elif-else conditions evaluates to true, and the message "You are an ordinary member." is printed. The following elif conditions, although potentially true, are ignored, because only one execution path in the if-elif-else sequence can be executed.

Note that to make a fallthrough algorithm work, the conditions must be checked in some kind of order - smallest to largest, largest to smallest, most likely to least likely, or visa versa. The following code would also be a working, fallthrough, algorithm:

    
if points >= PLATINUM:
    print('You are a Platinum member.')
elif points >= GOLD:
    print('You are a Gold member.')
elif points >= SILVER:
    print('You are a Silver member.')
else:
    print('You are an Ordinary member.')

  

Note how, as we move from the largest point count to the smallest point count, the condition check is no long <, it is the opposite, >=. So long as there is an order, the order chosen is irrelevant.