Skip to content

Latest commit

 

History

History
96 lines (70 loc) · 3.77 KB

File metadata and controls

96 lines (70 loc) · 3.77 KB

Lesson 2.03: Conditionals

Learning Objectives

Students will be able to...

  • Define and identify: if, else, elif, conditionals, flow of control
  • Create chaining if statements
  • Understand how conditional statements alter the flow of control of a program

Materials/Preparation

Pacing Guide

Duration Description
5 Minutes Do Now
10 Minutes Lesson
35 Minutes Lab
5 Minutes Debrief

Instructor's Notes

1. Do Now

  • Project the Do Now on the board, circulate around the class to check that students are working and understand the instructions.
  • Students should quickly realize that they do not have all the tools necessary to complete the task.

2. Lesson

Instruction

  • Explain that in order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly.

  • Conditional statements give us this ability to affect the flow of control.

  • The simplest form is the if statement. The Boolean expression after if is called the condition. If it is true, then the indented statement gets executed. If not, nothing happens.

    if x > 0:
        print("x is positive")

Take a look at this example

 animal = input("What is your favorite animal?")
 if animal == 'cat' or 'dog':
 print("A great pet!")
 else:
 print("Good choice")

Discussion

  • Give students time to predict the output for various inputs in the above example.
  • Discuss why the code is "buggy".
  • Fix it together as a class.

Demonstration

  • Write out the syntax of the if statement on the board. Point out the Boolean expression(condition), the colon, and the indentation.
  • else is used when there are two possibilities and the condition determines which one gets executed.
  • Demonstrate the syntax of else
  • Describe the elif statement.
  • Sometimes there are more than two possibilities and we need more than two branches. One way to express a computation like that is a chained conditional.
  • Demonstrate the syntax of elif.

3. Lab

  • Students write a triangle program in Python 3.
  • Students must also write a program that simulates a list index using if statements.

4. Debrief

  • Check student progress and completion of the lab. Wrap up by taking any final questions.
  • Have students write down in their notebooks a couple of key things that they learned today.