Module 4 - Branching Control Structures Header

Module 4 - Branching Control Structures

Introducing Control Structures and Branching

In Python, branching control structures are used to change the normal flow of execution based on some condition. There are three types of branching control structures in Python:

  • If statement: The if statement is used to check for a condition and execute a block of code if the condition is true.
  • Else statement: The else statement is used to execute a block of code if the condition in the if statement is false.
  • Elif statement: The elif statement is used to check for additional conditions if the condition in the if statement is false.


If Statements

If statements are an example of a branching control structure.  They allow your code to respond to things like variable values and user input by executing different blocks of code in different situations.

The following is an example of an if statement:

if x == 10:
     print("x is equal to 10")

In this example, the value of the variable x is checked to see if it is equal to 10. If it is, the block of code inside the if statement will be executed. Otherwise, the block of code will be skipped. See the diagram below for a visual representation of this:

I'd like you to notice a few other things about these lines of code, as well:

  • First, the line of code starts with the word if.
  • After the if, there is an expression that evaluates to true or false.
  • At the end of the first line, there is a colon(:)
  • All of the lines of code that will be executed if the expression is true are indented one level.  This will be refred to as a “code block”.
  • After the code block that goes with the if statement, the next line of code will be indented at the same level as the if statement itself.  This is where the program will pick up if the statement is false, or if it is true then it will pick up here following the execution of the lines of code in the code block under if.

Here is an example of what it would look like to put this if statement and a code block in a regular program:

print("This is a regular line of code")
if x == 10:
     print("x is equal to 10")
     print("This line also only gets printed if x is equal to 10")
print("This will be printed no matter what.")

So, if x is equal to 10 above, you will see the following when you run the program:

This is a regular line of code
x is equal to 10
This line also only gets printed if x is equal to 10
This will be printed no matter what.

If x is equal to anything else, then you would see the following:

This is a regular line of code
This will be printed no matter what.


Else Statements

The following is an example of an else statement:

if x == 10:
     print("x is equal to 10")
else:
     print("x is not equal to 10")

In this example, the value of the variable x is checked to see if it is equal to 10. If it is, the block of code inside the if statement will be executed. Otherwise, the block of code inside the else statement will be executed.

Here is a diagram that shows how this works:

Notice that in the code, the “if” line and the “else” line are both indented the same.  Also, the blocks of code beneath these lines are all indented one level more than the if and else lines.  This is how Python. knows that these statements and blocks of code go together.

A simple way to think about an if…else block is an “either/or” situation, where you are telling Python to “do thing A” if something is true, and to “do thing B” if it isn't.



Elif Statements

The following is an example of an elif statement:

if x == 10:
     print("x is equal to 10")
elif x == 20:
     print("x is equal to 20")
else:
     print("x is not equal to 10 or 20")

In this example, the value of the variable x is checked to see if it is equal to 10. If it is, the block of code inside the if statement will be executed. Otherwise, the value of x is checked to see if it is equal to 20. If it is, the block of code inside the elif statement will be executed. Otherwise, the block of code inside the else statement will be executed.

Notice that in the code block above, there are 3 different possible outcomes:

  1. X is equal to 10
  2. X is equal to 20
  3. X is equal to anything other than 10 or 20

Here is a visual flowchart showing how this block of code works:

Also, notice that the if and the elif lines have expressions, but the else line doesn't. This is because the else block is what gets executed if none of the expressions in if and elif are true.

But wait. There's MORE!

While you are always required to start out with an if, elif and else are optional. You can also have as many elifs as you want, with or without an else. So many possibilities!



Branching Summary

Branching control structures (if..elif…else) are an essential part of Python programming, and pretty much every other kind of programming as well. By understanding how branching control structures work, you can write more powerful and efficient code.

Here are some additional tips for using branching control structures:

  • Use indentation carefully.  Indentation means something in Python, and bad or inconsistent indentation will generate errors!
  • Use comments to explain what your code is doing.
  • Test your code to make sure it works correctly.

With a little practice, you'll be using branching control structures like a pro!



Try it: Branching

Player 1 and Player 2 are playing Rock, Paper, Scissors.  If Player 1 chooses Rock, then what happens if Player 2 chooses Rock, Paper or Scissors?

Can you write an if…elif…else block in the window below that will show the correct responses based on what Player 2 chooses?

 

Videos for Module 4 - Branching Control Structures

Introducing Control Structures ()

The If Statement ()

Comparison and Equality ()

Comparison Operators ()

Comparison Expressions ()

Boolean Operators ()

Else ()

Elif ()

M4 Sandbox ()

Branching Review ()

Command Line UI Concepts ()

Command Line UI Coding ()

A4 Explanation ()

A4 Code Walkthrough ()

Key Terms for Module 4 - Branching Control Structures

No terms have been published for this module.

Quiz Yourself - Module 4 - Branching Control Structures

Test your knowledge of this module by choosing options below. You can keep trying until you get the right answer.

Skip to the Next Question