Use conditionals to teach your program to make decisions based on the information it receives.
Conditional statements are computations that are performed based on the boolean values or bool that is created within the program. They help us to navigate trickier problems that may require the program to choose between multiple statements or skip over specific statements. These conditional statements are considered to be control structures, giving us flow control as they analyze and navigate the appropriate action to take.
A real-world example of conditional statements can be viewed through the eyes of a student:
“If I put in at least 2 hours of studying per weekday, I will increase my GPA.”
Based on the scenario above, our program’s result depends on whether the student’s studying condition was met. Because we are guiding the flow of this scenario, implementing a control structure such an if statement, would give us the type of response we’re looking for.
Very much in the likeness of a hypothesis in the scientific method, the structure of an if statement provides that:
If { a specific expression occurs }, then { an anticipated result will follow }.
In other words…
If { bool }, then { statement }.
The syntax for this in Python would resemble the following format.
Note: The code within each statement block should be indented one level until the end of that code. Let’s take a closer in the code below!
Now that we know what control structures are, let’s consider the possibility of multiple statements. In the code above, the print statement will only run if <study_hours >
is equal to 2. Meaning if the student studies less than or more than 2 hours per weekday, nothing will print onto the screen.
But what if we want the program to let us know whether the GPA will either increase or decrease? How would this look? We could write code that will perform like the pseudocode below:
If student enters more than 2 hours:
Print (“GPA is increased”)
Else:
Print (“GPA is not increased”)
The “else” prevents redundancy and implies that if the number entered is less than 2, the GPA will not be increased.
In order to code a program that will evaluate a condition and either choose one path if true or choose another path if false, we will need to create an if/else statement. To mentally map out your program, take a look at the flow chart below.
Now, let's view this in code!
There’s another way for the program to choose between several paths. To navigate 3 or more statements, we can use an an else if statement, otherwise known as elif. The program below shows that if the student studies more than 2 hours per day, their GPA increases. If they study exactly 2 hours per day, their GPA remains the same. If they study less than 2 hours per day, their GPA decreases.
To recap, conditionals are control structures that allow us to effectively navigate multiple outcomes. We have discussed the meaning of and how to use if, if/else, and elif statements. To check your understanding, take a look at the vocabulary list below and try to recall how each term is either used or its definition.
Vocabulary Terms
Try It Out!
Practice is never a bad thing! Feel free to manipulate the code editor below to get a better grasp of syntax and using conditionals in general. Are there any other real-world situations that navigate the flow of If, If/Else, and Elif statements? For an extra challenge, create code for the following scenarios!
Challenge Scenarios
Hint: Remember to store your boolean expressions into variables.