Lesson 6: Conditionals Investigate

App Lab

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.

Agenda

Lesson Modifications

Warm Up (5 mins)

Activity (35 mins)

Wrap Up (5 mins)

View on Code Studio

Objectives

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

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.

For the Teachers

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 mins)

Preview the Lesson

Discussion Goal

Optional Warmup Prompt: This prompt 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 prompt.

Prompt: 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.

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 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 mins)

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

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.

Investigation 1: If-Statements (Levels 2-3) - 15 mins

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

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 3 - 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.
  • 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 (Levels 4-6) - 10 mins

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

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

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 modifications to the code. Continue to emphasize open discussion and collaboration between partners.

Level 6 - Lemon Squeeze App Pt 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-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 the code: Modify the program to make the lemon even smaller when the player has more than 15 points.

Investigation 3: Logical Operators AND OR (Levels 7-8) - 10 mins

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

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.

Level 8 - Lemon Squeeze App Pt 3: This program is a final version of the Lemon Squeeze app. Again students should work with 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 if-else-if command that was added
  • Draw a Flowchart: Students should draw a flowchart of this new if-else-if 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 mins)

Discussion Goal

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.

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

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?

Standards Alignment

View full course alignment

CSTA K-12 Computer Science Standards (2017)

AP - Algorithms & Programming
  • 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.

CSP2021

AAP-2 - The way statements are sequenced and combined in a program determines the computed result
AAP-2.H - For selection: a. Represent using conditional statements. b. Determine the result of conditional statements.
  • 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 - For nested selection: a. Represent using nested conditional statements. b. Determine the result of nested conditional statements.
  • AAP-2.I.1 - Nested conditional statements, or “else if” statements, consist of conditional statements within conditional statements.