< Unit 4 - Variables, Conditionals, and Functions ('21-'22)

Lesson 6: Conditionals Investigate

45 minutes

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.

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.

CSP Conceptual Framework
      • AAP-2.H.1 - Conditional statements or “if-statements” affect the sequential flow of control by executing different statements based on the value of a Boolean expression.
      • AAP-2.I.1 - Nested conditional statements, or “else if” statements, consist of conditional statements within conditional statements.
CSTA K-12 Computer Science Standards (2017)
    • 2-AP-10 - Use flowcharts and/or pseudocode to address complex problems as algorithms.
    • 2-AP-12 - Design and iteratively develop programs that combine control structures, including nested loops and compound conditionals.
    • 3B-AP-23 - Evaluate key qualities of a program through a process such as a code review.

Agenda

Objectives

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

Preparation

  • Review the example apps and the prompts that students will be asked to respond to for each
  • Review the information covered in the slides

Links

Heads Up! Please make a copy of any documents you plan to share with students.

Teaching Guide

Lesson Modifications

Attention, teachers! If you are teaching virtually or in a socially-distanced classroom, please read the full lesson plan below, then click here to access the modifications.

Warm Up (5 minutes)

Preview the Lesson

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 (&&, ||, !) when you write your Boolean expression.

Optional Warmup Discussion: This discussion is optional and helpful 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 discussion.

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 onEvent to respond to user input. 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 minutes)

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.

Level 1 - Video - Conditionals: If Statements As a class watch the video on if-statements.

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.

Level 2 - Lemon Squeeze App Pt 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.

  • Discuss: 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.

    • Discussion Goal: The goal of this investigation is to get practice carefully reading a program that uses the new concept of conditionals as well as build on their knowledge and understanding of variables.

    • What to look for: As students describe their section of code, push students to go beyond reading the comments in their section of code to describe what their code does. For example, instead of saying “It increases the score by one”, students should explain HOW that happens. They might say “The value stored in the score variable increases by 1 and then the setProperty is used to show the increased score on the screen”.

  • 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 and MOD

Level 3 - Video - Conditionals: If-Else Statements As a class watch the video on if-else statements.

Level 4 - Lemon Squeeze App Pt 2: This code investigation includes a number of steps to help students get familiar with a new app.

Do This: Now look together at lines 54-58. Students discuss with a partner how they think the MOD operator works.

Teaching Tip

Students need to know how the MOD operator works for the AP CSP exam. The explanations here are enough to help them answer those questions.

There are many wonderful and interesting uses of MOD in apps, but students are not expected or required to use the operator themselves when programming apps for this course.

Do This: The MOD operator is just like any other operator - it takes two numbers, processes them, and then returns a value. Click through the animations one by one as students give answers to each arithmetic statement.

Do This: Give students a few minutes to practice using the MOD operator with a partner, solving the problem on the screen. Then click through to see the answer.

Remarks

MOD can be a little tricky, but here's what you need to remember: it's the reaminder that is left after a number is divided by another number.

Why is this useful? A common usage is to determine if a number is even or odd. If you divide any number by two and there is no remainder, the number is even! You can also use MOD to determine if a number is divisible by another number.

Now let's get back to another app investigation with if-else-if statements.

Investigation 3: If-Else-If Statements (10 minutes)

Level 5 - Video - Conditionals: If-Else-If Statements As a class watch the video on if-else statements.

Level 6 - Lemon Squeeze App Pt 3 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

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-else-if statement and make more significant modfications to the code. Continue to emphasize open discussion and collaboration between partners.

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

Investigation 3: Logical Operators AND OR (10 minutes)

Level 7 - Video - Conditionals: If-Else Statements As a class watch the video on if-else statements.

Level 8 - Lemon Squeeze App Pt 4: This program is a final version of the Lemon Squeeze app. Again students should work with partners.

Teaching Tip

Running the Third Investigtaion: 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-else-if command: Have students find the if-else-if command that was added
  • Draw a Flowchart: Students should draw a flowchart of this new if-else-if command
  • Modify: 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)

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

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-else-if statements can check more than one boolean expression. They will only run the code for the first boolean expression that evaluates to true.

Review Patterns: Have students read the pattern introduced in Level 9. This pattern appears frequently with if-else-if statements. Students' understanding of this pattern is assessed in the Check Your Understanding Question.


Assessment: Check For Understanding

Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.

Question: When creating an if-else-if 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?
Creative Commons License (CC BY-NC-SA 4.0).

This work is available under a Creative Commons License (CC BY-NC-SA 4.0).

If you are interested in licensing Code.org materials for commercial purposes contact us.