If this than that

If this than that#

Another important concept in programming is to change the workflow of a program based on its state or input.

Think of you’re running a simulation and like to stop iterating as soon as an error, which you would consider a measure for result quality, drops below a certain threshold. That’s the general idea behind most simulations. Repeat some sort of calculations until the result does no longer change from iteration to iteration. The difference in results between two iterations in your program can be such an error, which needs to be as low as possible for reasonable result quality.

Most simulation engines supporting your ideas work that way. You have to write an algorithm in a way that can be optimized. And optimization in this case means that a resulting error needs to be minimized in order to complete.

So in every iterative simulation a decision has to be made at some point. Are we ready yet?

Python supports this programming concept with the if clause. The idea behind this is like this:

if some expression evaluates to True, then do something, else if some other condition evaluates to True, then do different things, else do someting else. You can have one single if clause, as many else if branches as you desire, also none, and a single else branch, which is also optional.

Let’s have a closer look.

Imagine we’re doing a one step simulation to check if a randomly chosen number is below a certain threshold, say 0.5. We can use a method from the random module to generate a (pseudo-) random number, and then check if the number is below that threshold.

We’ll import the random module.

import random

Next, we set the lower threshold.

lower_threshold = .5

And finally we create the random number. In the if clause the expression number < lower_threshold will either evaluate to True or False. In case the result is True the following code will be executed. If the result is False the else branch becomes active.

number = random.random()

if number < lower_threshold:
    print(f"The number {number} is below the lower threshold {lower_threshold}.")
else:
    print(f"The number {number} is above the lower threshold {lower_threshold}.")
The number 0.2901024564597935 is below the lower threshold 0.5.

A more sohisticated example would check for a lower and upper bound. In this example we make use of the elif branch to allow a second check. We’ll set a lower and an upper threshold first.

lower_threshold = 0.25
upper_threshold = 0.75

And totally comparable to out first example the expression number < lower_threshold might again to either True or False. In case of being True the directly following code block is executed. Is the result False we’re arriving at the elif statement, which triggers another check. Here number > upper_threshold might evaluate to True. In this case the following code is executed. Is the result False on the other hand, you already guessed it, the else branch is executed.

number = random.random()

if number < lower_threshold:
    print(f"The number {number} is below the lower threshold {lower_threshold}.")
elif number > upper_threshold:
    print(f"The number {number} is above the upper threshold {upper_threshold}.")
else:
    print(f"The number {number} is in between the lower threshold {lower_threshold} and the upper threshod {upper_threshold}.")
The number 0.6300519409240011 is in between the lower threshold 0.25 and the upper threshod 0.75.

A simple if clause without any elif or else is also quite commonly used. The typical pattern here is to update some default value set before with a new value because of a changed condition. Think of some default variable which can be overwritten because of entries in a configuration file, or command line arguments to the program at start time. You often could use an else clause in these scenarios as well. The consequence however would possibly be to have a rather long code block indented because of this, right behind a quick and small if check. That will end up in not being very readable, and should therefore be avoided. The following example might illustrate this.

command_line_options_dont_create_plots = True
create_plots = True
if command_line_options_dont_create_plots:
    create_plots = False
    
# lot's of simulation code here

if create_plots:
    print("Creating plots now")