Lesson 3: Nested if Statements
45 minutes
Overview
How can I test a combination of conditions before executing a specific action?
Students have used if
and if-else
statements to execute blocks of code based on a condition. In this lesson, students explore the functionality of nested if
statements and learn to use these to test a combination of conditions. Students use nested if
statements to write an algorithm to compare pairs of values in a 1D array.
Standards
CON-2 - Programmers incorporate iteration and selection into code as a way of providing instructions for the computer to process each of the many possible input values
CON-2.B - Represent branching logical processes by using nested conditional statements.
- CON-2.B.1 - Nested if statements consist of if statements within if statements.
Agenda
Objectives
Students will be able to:
- Evaluate code segments using nested
if
statements - Write a nested
if
statement to test a combination of conditions
Preparation
- Check the Teacher's Lounge for verified teachers on the CSA Forum to find additional strategies or resources shared by fellow teachers
Links
Heads Up! Please make a copy of any documents you plan to share with students.
For the students
- U4L3 Extra Practice - Handout
Vocabulary
- nested
if
statement - anif
statement inside anotherif
statement
Teaching Guide
Warm Up (10 minutes)
Data Visualizations
Remarks
For the unit project, you will use The Theater to create visualizations of data. Data visualizations help us make sense of data and communicate its story. These visuals are a powerful way to discover and understand these stories and present them to others. Let's take a look at an example.
Discuss: Click through the animated slide to display the prompts.
- What story do you think this data visualization is trying to tell?
- Why do you think the creator chose to visualize the data in this way?
Discussion Goal: Students share opinions and ideas about what they think the data visualization is trying to convey and why the creator chose this visual.
Ask students what other visuals the creator could have used to convey the same story. Have students consider how alternative visuals would have impacted the effectiveness of the message.
Activity (30 minutes)
Nested if Statements (15 minutes)
Remarks
In the previous unit, we learned about the term nested when referring to a loop inside another loop. Let's look at how nesting works with if
statements.
Do This: Review the lesson objectives.
Group: Place students in pairs.
Do This: Direct students to Level 1 on Code Studio to investigate the program with a partner. Students make the changes to the program as prompted.
Discuss: Click through the animated slide to display the prompts.
- What do you notice about the code in this program?
- What do you wonder about the code in this program?
Discussion Goal: Students notice the nested if
statements and that both the outer and inner if
statements have to be true
for the code to execute. Students may wonder if they can also nest if
statements inside the else
statement or if there are other ways to check multiple conditions.
Do This: Click through the animated slide to demonstrate a nested if
statement and the flowchart for the code segment.
Discuss: Use the Hold That Thought strategy to discuss the prompt.
- Why would we have an
if
statement in the body of anotherif
statement?
Discussion Goal: Students suggested using nested if
statements to allow the program to check conditions within other conditions. Students may share scenarios where they would want to do this, such as checking if someone has enough money for a movie ticket and an available movie time.
If students don’t get to this on their own, ask the following guiding questions:
- What is meant by nested?
- How is this nested statement similar to a nested loop?
- What are some scenarios where you would need to check if multiple conditions were
true
?
Using Nested if Statements (15 minutes)
Remarks
We can use nested if
statements to check if multiple conditions are true before executing a block of code. When we are making decisions about how to visualize data, we might choose to display images and shapes based on multiple conditions. Let's see how we can use nested if
statements to analyze data and display visuals.
Group: Place students in pairs.
Students can be in the same pairs as the Investigate activity.
Do This: Have students write pseudocode for an algorithm to decide which image to draw based on the given conditions using nested if
statements.
Do This: Direct students to Level 2 on Code Studio to complete Levels 2 and 3. Students implement their algorithm on Level 2, then complete a choice level on Level 3 to use nested if
statements in different scenarios.
Wrap Up (5 minutes)
Telling a Story
Remarks
In the previous unit, we worked with data as 1D arrays to answer questions and find information.
Discuss: How might you tell the story behind the data you worked with in the previous unit as a visualization?
Discussion Goal: Students share ideas about how they might visualize the data they worked with in the previous unit, such as displaying shapes in different sizes to correlate with the number of values that meet specific criteria or convey the largest and smallest values in a dataset.
Have students consider how they would create these visualizations in The Theater. Ask students if they would create a static image or an animation and how these approaches would impact the effectiveness of the message.
Do This: Review the concepts covered in this lesson.
Display: Key Vocabulary
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.
AP Classroom Topic Questions
To assign questions from the AP Classroom Question Bank that align with this lesson, create a custom quiz in AP Classroom by searching the Question Bank for the Essential Knowledge statements listed at the top of this lesson plan. You can find instructions and video demonstrations to do this on AP Central.
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.