Decision structures are an extremely powerful component of programming languages, and using them correctly is key to creating correct programs. To understand decision structures, I’m going to start by presenting a problem that requires a decision. From here, we will see how this decision can be programmed, and understand what the program is doing when it is run.
The first problem relates to the weather. Suppose we wanted to create an application that would determine if a user should bring an umbrella with them. If the weather forecast says there will be rain, we will tell the user to bring an umbrella. Otherwise, we will tell the user not to bring an umbrella. Let’s think about how this sort of problem can be programmed.
- We ask the user what the weather forecast is
- We then check the weather forecast inputted:
a. If it is “rain”, we print “Bring an umbrella”
b. If it is not “rain”, we print “Don’t bring an umbrella”
You can see that there is a branch where we decide an action based on the input. If the input is “rain”, we do one thing, and if it isn’t “rain”, we do a completely different thing. This sort of idea is easiest to see if we draw it out, in the form of a flowchart.
In this flowchart, you can easily see how the paths branch based on the input the user provides. This is the idea of decision structures; they allow us to make choices based on the data we have. Let’s take this example, and implement it in Python to see what a simple decision structure looks like.
You can see that the first line involves us asking the user to input the weather. This result is stored inside of weather to be accessed later in the program. The next line is the first component of the decision structure mentioned above. You can think of it as the diamond shape in the flow chart, which asks the question “Did you user input rain?”. It checks if the variable weather is equal to “rain”. The == indicates checking for equality. The next line is indented to indicate that it happens only on the condition that the if statement is true. In the diagram, it is equivalent to the branch arrow that says “yes”. If the condition is true, we print “bring an umbrella”. We then proceed passed the else and go to the end of the program. The else portion is indicating what we do in case we get an input that is not equal to “rain”. This would be equivalent to the branch arrow that says “no” in our diagram, which prints “Don’t bring an umbrella”.
This gives you a basic understanding of how we can use decision structures in Python. Next, let’s take a look at a more complex example, where we have multiple conditions to check. Suppose we wanted to create a grade conversion program for school. The user enters a percentage grade, and we want to convert it into a letter grade, and display that result to the screen. The letter grade is calculated as follows:
1. If the percent grade is between 80 and 100, the letter is A.
2. If the percent grade is between 70 and 79, the letter is B.
3. If the percent grade is between 60 and 70, the letter is a C.
4. Anything below 60 is a D.
First, let’s make up a flowchart so we can see how this process flows before we attempt to program it.
From this diagram, it is clear to see how the program will flow. Now, we can translate our flowchart into Python code.
There are a few differences between this and our first example that are important to note. The first is that instead of using else after the if statement, we are using elif. The purpose of elif is to check a condition if the first condition fails. So in this case, we first check if the grade is greater than or equal to 80. If it is, we print “A”, if not, we check another condition, which is if the grade is greater than or equal to 70. If this fails, we then check if the grade is greater than or equal to 60. Finally, if all the conditions fail, we print a letter grade of “D”.
You may recall in the initial problem, we stated that grades from 70–79 would be given the letter “B”. You may notice that in our program, we only check if the grade is greater than or equal to 70. The reason that this works is because we only get to this check if we fail the first if statement. That means we know the grade is not greater than or equal to 80. Meaning it would be at most 79. From here, we just check if it is greater than or equal to 70, giving us a value between 70 and 79.
To summarize, if statements allow us to check conditions while executing our program to create branching logic. Else is a keyword that will execute in any case where the initial if condition is not true. Elif is a keyword that executes when the initial if condition is not true, and additionally checks a new condition to decide if the branch applies. If statements appear in many contexts and real-world scenarios, so understanding them is key to writing programs.