Variables, Conditionals, and Functions - Lesson 6: Conditionals Investigate

Overview

In this lesson students work with partners to investigate three versions of the "Lemon Squeeze" app to understand how boolean expressions and conditional statements allow programs to make decisions. In each guided investigation students first watch a short video on a concept, then use a working app to predict how new features work, then investigate the code to see how those features are implemented, and finally modify the code to add expanded features. To conclude the lesson, students review and discuss common programming patterns with conditionals.

Goals

Students will be able to:

  • Identify common programming patterns using boolean expressions and conditional statements
  • Explain the purpose of those programming patterns with boolean expressions and conditional statements both in terms of how they work and what they accomplish
  • Modify apps that make use of common programming patterns with boolean expressions and conditional statements to adjust their functionality

Purpose

After building a conceptual model for boolean expressions and conditional statements in the previous lesson, this lesson allows students to see how they are actually implemented in code. This lesson also introduces common programming patterns when using variables. Students will have some opportunities to modify working code in this lesson, but the most significant practice with conditional statements and boolean expressions will come in the following lesson.

Resources

Getting Started (5 minutes)

Discuss: A water park will let a visitor on a ride if they are 48 or more inches tall OR they are 14 years old or older. Make a flowchart for this decision. Make sure to use comparison operators (<,>,=, etc.) and logical operators (and,or,not) when you write your Boolean expression.

Teaching Tip

Optional Warmup Prompt: This prompt is optional and is helpful to further synthesize key points from the previous lesson. If you are able to quickly move to the main activity and believe your class does not need this revew consider skipping this prompt.

Discuss: Have partners share their responses at their tables. Then discuss answers briefly as a class.

Remarks

  • In everyday conversation it is common to switch between using the words "when" and "if". Here's some examples of what this looks like:
    • "When the user clicks the button..."
    • "If the user clicks the button..."
    • "When the user has more than 100 points..."
    • "If the user has more than 100 points..."
  • Today we want to be careful about how we use these words. To make things simple, we're going to use the following rules:
    • "when": Means there is an action that responds to user input, like ButtonActivated. The app does something "when" the user clicks.
    • "if": Means there is a conditional statement that decides what pieces of code to run. The app does something "if" a boolean expression evaluates to true.
  • We'll talk more about this later but for now keep an eye out for the difference between "when" and "if".

Activity (35 minutes)

Group: Place students in pairs. One student per group should navigate to the lesson on Code Studio.

Investigation #1: If Statements (15 mins)

Teaching Tip

Running the First Investigation: This first investigation is supposed to get students moving around the room while also motivating them to carefully read a new program. As you circulate the room encourage them to read carefully, ask good questions, and be comfortable asking where they don't understand something.

Display: As a class, watch Introduction to Conditionals (Video, Part A).

Teaching Tip

Show the Video at the Front: In order to better keep the group together we recommend you show the videos at the front of the room.

Reinforce When vs. If: As you make your way through these examples model using "when" and "if" as described in the warm up. For example, "when" the buttons are clicked the app is deciding "if" the temperature can change.

Open a Project: Direct students to open Lesson6_App1.

Activity - Lemon Squeeze App (Part 1): This code investigation includes a number of steps to help students get familiar with a new app.

  • Form Pairs: Place students in pairs
  • Play the Game: Let students play the game for a couple minutes
  • Assign Code Sections: Count off pairs by three and assign them to one of the three code sections.
  • Read Code: Groups should carefully read the code for their section making sure they understand how it works. Give them 3 minutes to do so.
  • Explain Your Section: Have groups find members of the other two sections and carefully explain how their section works. Give each group 1-2 minutes to do so.
  • Class Discussion: Ask a few members of each section to quickly share out how their section works. Display the code at the front so you can talk through it together.
  • Modify: Have groups return to their original seats. Give them a couple minutes to work on the modify the app so that the game ends once a user has 0 lives remaining.

Investigation #2: If-Else Statements (10 mins)

Display: As a class, watch Introduction to Conditionals (Video, Part B).

Display: As a class, watch Introduction to Conditionals (Video, Part C).

Open a Project: Direct students to open Lesson6_App2.

Activity - Lemon Squeeze App (Part 2): This program is an updated version of the Lemon Squeeze app. This time students should continue to work in partners but do not need to work with other groups. They will need to:

  • Play the Game: Have students play the game
  • Discuss Changes: Talk through how the game plays differently now
  • Find the if-elseif command: Have students find the command
  • Draw a flowchart: Students should draw a flow chart for the if-elseif command in their journals
  • Modify the code: Modify the program to make the lemon even smaller when the player has more than 15 points.
Teaching Tip

Running the Second Investigation: This second investigation remains focused on reading code, but students are more responsible than last time for showing their work. Students will need to create a flowchart for the new if-elseif statement and make more significant modfications to the code. Continue to emphasize open discussion and collaboration between partners.

Investigation #3: Logical Operators AND OR (10 mins)

Display: As a class, watch Introduction to Conditionals (Video, Part D).

Open a Project: Direct students to open Lesson6_App3.

Activity - Lemon Squeeze App (Part 3): This program is a final version of the Lemon Squeeze app. Again students should work with partners.

Teaching Tip

Running the Third Investigation: In this final investigation students are primarily working independently to design their flowchart and modify the code. While you should quickly have students share out what they noticed about how the app changed, spend most of your time circulating answering questions students have as they modify the programs or draw their flowcharts.

  • Play the Game: Have students play the game
  • Discuss Changes: Talk through how the game plays differently now
  • Find the if-elseif command: Have students find the if-else-if command that was added
  • Draw a Flowchart: Students should draw a flowchart of this new if-elseif command
  • Modify the code: Modify the program to add a new username and password. Students will have to modify the code as well as the user interface.

Wrap Up (5 minutes)

Discuss: What is the difference between an if-statement, an if-else statement, and an if-elseif statement? How are they similar?

Teaching Tip

Discussion Goal: Some key points to call out in this discussion.

  • These statements are more the same than different. They all use boolean expressions to decide whether to run certain pieces of code.
  • if-statements check if one boolean expression is true. If it is, a piece of code is run. Otherwise the code runs as normally.
  • if-else statements add the functionality that if the condition is false it can still run some code before returning to running the program as normal
  • if-elseif statements can check more than one boolean expression. They will only run the code for the first boolean expression that evaluates to true.

Review if-elseif: Review how to check multiple conditions by discussing the examples and documentation below.

Checking Multiple Conditions with if-elseif

if temperature > 100
    output "It's dangerously hot"
elseif temperature > 90
    output "It's very hot"
elseif temperature > 80
    output "It's hot"
elseif temperature > 70
    output "It's warm"
else
    output "It's cool"
end

How does it work?

The if-elseif command lets you check multiple boolean conditions. The computer will check conditions in the order they are written until one of the boolean expressions evaluates to true. The code associated with that boolean expression will be run, but all others will be skipped. If none of the expressions evaluates to true then the code inside the else command will be run.

Most Specific Cases First

When writing an if-else-if statement you want to put the most specific cases first. In the temperature example above you want to check for temperatures over 100 degrees first. Afterwards the code checks for temperatures above 90 degrees, but because of the order the code is written you know that none of temperatures you'll find there are above 100 degrees. After all you would have caught them in the previous if-statement. This means you can be confident any temperatures you catch there will be between 90 and 100 degrees. As you continue through the if-else-if statement you use the same logic to check for different ranges of temperature.

A Broken Example: Most Specific Cases Last

This is a broken example that shows what happens if you start checking for temperatures in reverse order, most specific cases last.

if temperature > 60
    output "It's cool"
elseif temperature > 70
    output "It's warm"
elseif temperature > 80
    output "It's hot"
elseif temperature > 90
    output "It's very hot"
else
    output "It's dangerously hot"
end

Think through what would happen when this code runs for the temperature 82 degrees. You would want the output to say "It's hot". If you look at the first boolean expression, however, you'll notice that 82 is higher than 60 degrees, making that boolean expression evaluate to true. As a result the code will output "It's cool". This is because the first expression is not actually the most specific.

Assessment: Check for Understanding

For Students

Open a word doc or google doc and copy/paste the following question.

Question

When creating an if-elseif statement you should always make your first condition the most specific. Write a short paragraph responding to the questions below.

  • What does it mean to put the most specific case first?
  • Why is it important to put the most specific case first? What types of errors does it help avoid?

Standards Alignment

  • CSTA K-12 Computer Science Standards (2017): 2-AP-10, 2-AP-12, 3B-AP-23
  • CSP2021: AAP-2.H.1, AAP-2.I.1

Next Tutorial

In the next tutorial, we will discuss CSP Unit 4 Lesson 7 Conditionals Practice, which describes Practice using conditionals.