Branching

Learn to use conditions in if, elif, and else statements to control the flow.

Grades 8+
  • Web
Request Quote
for Middle School Curriculum

Lesson Plan

Lesson: Branching

Time: 40+ mins

Introduction

In this lesson, students will use conditions in “if,” “elif,” and “else” statements to control the flow.

Vocabulary

  • Branch: A statement that determines the sequence of instructions that a program executes. Programs may follow one branch or another depending on the result of the boolean/conditional statement.
  • Condition: A boolean expression that evaluates to either True or False. Conditions can also just be the boolean values True or False.
  • “If” Statement: A statement containing the word "if" followed by a boolean/conditional expression and a colon. If the boolean expression is true, the indented code following the “if” statement will be executed. If the boolean expression is false, the indented code will not execute and the next non-indented instruction will be executed. The end of the “if” statement must have a colon and there must be at least one indented instruction after the “if” statement. “If” statements can be nested as long as the amount of indentation stays consistent. Each level of indentation is suggested to be one tab or 4 spaces. The “if” statement in Python has the following syntax:
  • "Else" Statement" A statement that is directly after the set of indented code following an “if” statement. The indented code following the “else” statement will execute if the condition in the “if” statement is false. The “else” statement in Python has the following syntax:
  • “Elif” statement: A statement that can be placed between an “if” statement and an “else” statement. The “elif” statement should contain a condition and a colon, just like the “if” statement. However, the “elif” statement is only executed if the previous “if” or “elif” condition is false. There can be numerous “elif” statements. The “elif” statement in Python has the following syntax:
        

Objectives

Students will...
  • Create and use conditional statements
  • Write programs that react differently based on user input
  • Use “if,” “elif,” and “else” statements
  • Create more complex decisions by nesting “if” statements
  • Apply branching concepts to real world problems

Materials

  • Computers (1 per student) with student account access to Tynker.com

Warm-Up (5 minutes)

Lead a short discussion with your students:
  • Explain to your students that this warm-up is intended to get them thinking about sequencing and conditional logic.
  • Ask the class for step-by-step directions on how to get from the back of the classroom to the classroom’s front door.
  • Ask, “What should you do if there is an obstacle (e.g., chair, desk) in front of you?”
  • Ask, “If there is not an obstacle in front of you, what should you continue doing?”

Activities (35 minutes)

Facilitate as students complete the Branching modules on their own:
1. If Statements (Tutorial)
    • Students will read a short document that explains “if” statements.
    • Are students struggling with the “Happy hundredth” puzzle? Tell them to use the code in the boolean expression example as a reference.
    • Is the animated branching description moving too quickly? Click the pause button to stop the animation, and click it again to continue playing.
    • Are students struggling with the “Type of angle” puzzle? Tell them to use the code in the “Number properties” example as a reference.
    • Tell students to click the “Next” button (located at the bottom of the document) to move on to the next module.
2. If Statement Syntax (Tutorial)
    • In this module, students will need to identify and correct errors in the given code.
    • Explain to students that the most common syntax errors with “if” statements involve indentation and placement of the colon (:).
    • Note: Students are provided information on how to fix the syntax errors.
3. Conditions (Tutorial)
    • Students will read a short document that explains conditions.
    • Check that students are playing the examples.
    • Optional: Ask students “Is ‘False’ equivalent to 1 or 0?” (0) Note: False is also equivalent to the empty string: “”
    • Encourage students to experiment and slightly modify the given code in the “Buried treasure” example.
    • Are students struggling with the “Even or Odd” puzzle? Give a hint: Tell them to use a modulus operator (%), which returns the remainder after dividing the left value by the right value.
    • Tell students to click the “Next” button to move on to the next module.
4. Else Statements (Tutorial)
    • Students will read a short document that explains “else” statements.
    • Optional: Before students select the “play” button on the “else” statement example, ask them to write down what they think the code will output.
    • Are students struggling with the “Password protection” puzzle? Give a hint: Tell them to use a modulus operator (%).
    • Tell students to click the “Next” button to move on to the next module.
5. Else Statement Syntax (Tutorial)
    • In this module, students will need to identify and correct errors in the given code.
    • Remind students that an “else” statement must be placed directly after the indented instruction of an “if” statement.
    • Note: Students are provided information on how to fix the syntax errors.
6. Elif Statements (Tutorial)
    • Students will read a short document that explains “elif” statements.
    • Encourage students to slightly modify the code in the provided example. How do their changes affect the output?
    • Are students struggling with the “Plus and minus grades” puzzle? Give a hint: Tell students that their solution will likely contain more than 25 lines of code. Also, tell them to use the code in the first example as a reference.
7. Nested If Statements (Tutorial)
    • Students will read a short document that explains how to apply nested “if” statements.
    • Optional: Before students select the “play” button on the first example, ask them to write down what they think the code will output.
    • Are students struggling with the “Rock Paper Scissors” puzzle? Give a hint: Tell students to try using an “elif” statement.
8. Play Against a Computer (DIY)
    • In this module, students will need to create two clever solutions and then click the “submit project” button so you can see their work!
    • If students are feeling discouraged about finding a solution that works, tell them to use the code and information in this module as a reference.
    • Optional: Encourage students to brainstorm possible solutions with a neighbor.
9. Quiz
    • This quiz requires students to apply concepts from this lesson to solve 3 different puzzles. There are no multiple choice questions.

Discussion Questions/Follow-Up Activities (20 minutes)

Lead a discussion with your students:
    • Who can list and describe some conditional statements we used today?
    • True or false: A branch is a statement that determines the sequence of instructions a program executes. (True)
    • Who can describe “branching” in their own words?
    • What did you or someone you know find tricky about today’s lesson?

US Standards

  • CCSS-ELA: SL.8.1, RI.9-10.3, RI.9-10.6, RI.11-12.3, RI.11-12.6, L.9-10.3, L.9-10.6, L.11-12.3, L.11-12.6
  • CCSS-Math: HSN.Q.A.1, HSN.Q.A.2, HSN.Q.A.3, MP.1, MP.2, MP.4
  • CSTA: 2-AP-11, 2-AP-13, 2-AP-16, 2-AP-17, 3A-AP-17, 3B-AP-11, 3B-AP-22
  • CS CA: 6-8.AP.11, 6-8.AP.13, 6-8.AP.16, 6-8.AP.17, 9-12.AP.12, 9-12.AP.16
  • ISTE: 1.c, 1.d, 4.d, 5.c, 5.d, 6.b
Branching
Python 201
Lesson Includes
  • 9 Activities
  • 1 Completion Badge
Languages
Python
Experience
Advanced