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.
Students will be able to:
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.
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 (
not) when you write your Boolean expression.
Discuss: Have partners share their responses at their tables. Then discuss answers briefly as a class.
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.
ButtonActivated. The app does something "when" the user clicks.
Group: Place students in pairs. One student per group should navigate to the lesson on Code Studio.
Display: As a class, watch Introduction to Conditionals (Video, Part A).
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.
Teaching Tip: 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 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:
if-elseifcommand: Have students find the command
Teaching Tip: 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.
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.
if-elseifcommand: Have students find the if-else-if command that was added
Teaching Tip: 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.
Prompt: What is the difference between an if-statement, an if-else statement, and an if-elseif statement? How are they similar?
Discussion Goal: Some key points to call out in this discussion.
Review if-elseif: Review how to check multiple conditions by discussing the examples and documentation below.
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
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.
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.
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.
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.