Lists, Loops, and Traversals - Lesson 7: Loops Practice
Overview
Students practice the basics of loops including using while loops, repeat times loops, and updating multiple screen elements with a while loop. Along the way students develop debugging practices with loops.
Goals
Students will be able to:
- Write programs that use loops with the support of sample code.
- Debug programs that use loops
- Use a for loop to update multiple screen elements at once
Purpose
This lesson is students primary opportunity to get hands on with loops in code prior to the Make activity in the following lesson. Give students as much class time as you can to work through these. For this lesson it's recommended that you place students in pairs as a support and to encourage discussion about the challenges or concepts they're seeing.
Resources
Getting Started (5 minutes)
Teaching Tip
Move Quickly to the Activity: There's a lot in the main activity of today's lesson. You may optionally wish to do a quick vocabulary review or address any questions that came up in the last lesson. Otherwise, give students more time to get hands on with some code.
Quick Warm Up
- Today we're going to have a chance to practice programming with a lot of the concepts and patterns we've explored over the last two lessons. I encourage you to go through these with a partner, but pay close attention to what each other is doing. In our next lesson you're going to have to use a lot of these on an independent project, and these activities are good practice for what you'll find there! Alright, let's get to it!
Activity (35 minutes)
Debugging
- Describe the Problem: What do you expect it to do? What does it actually do? Does it always happen?
- Hunt for Bugs: Are there warnings or errors? What did you change most recently? Explain your code to someone else. Look for code related to the problem
- Try Solutions: Make a small change.
- Read the resources in the Help & Tips tab
- Document As You Go: What have you learned? What strategies did you use? What questions do you have?
Practice Time
Teaching Tip
Providing Support: Circulate around the room through the lesson encouraging students to use the strategies introduced at the beginning of the lesson. Students have a number of supports at their fingertips, so a big part of your role is helping build their independence in using those resources.
Remarks
- Today you're mostly going to practice what we've learned about programming with loops. I'm here to help you when you need. However, I first want to remind you of the following:
- Use your debugging skills. Try to zoom in on precisely where you're getting stuck.
- Talk to your partner! That's what they're there for!
- Hover over blocks to read the documentation about how they work.
- Read the resources in the Help & Tips tab
- Talk to the group next to you. If another group asks for help make sure to take some time to talk it through with them.
- We can debug loops by using the Watch Panel with our iterator variable, usually an i.
- Watcher Panel: Here you can see the length of a list in addition to all of the elements.
Open a Project: For today's activity, students will work through a series of projects, beginning with Lesson7_App1.
Apps 1-3 While Loop Practice: Simple output
projects in which students practice using while loops.
- App 1: Students use a while loop to print the numbers 0-99
- App 2: Students use a while loop to print a message 100 times
- App 3: Students use a while loop to fill an array and print the results
Apps 4-6 For Loop Practice: In these levels, students consider when to use for
loops, and when they should use while
loops instead.
- App 4: Students use a for loop to print the numbers 0-99.
- App 5: Students use a for loop to print a message 100 times.
- App 6: Students use a for loop to fill an array and print the results.
Apps 7-8: Loops and Screen Elements: In these levels students practice writing programs that modify screen elements with loops. Screen elements have already been designed to have repeated patterns (e.g. "dice0", "dice1", "dice2" ...) that make it possible to write code that modifies every screen element. This is important practice for the Make project in the next lesson.
- App 7: This project is a simple introduction to using loops to modify multiple labels at once.
- App 8: Students must update a "Dice Roller" app that has some working functionality already. All of the different concepts they'll need to use are already in the starter code of this app, but they'll need to think through how to put them together to get the target code behavior.
Wrap Up (5 minutes)
Prompt: What aspects of working with loops do you feel like clicked today? What do you still feel like you have trouble with?
Discussion Goal
Use this opportunity to address any lingering questions or misconceptions in the room. You can also use this as a source of discussion topics to kick off the following lesson. As you lead the discussion, call out the many resources students have access to help when they're getting stuck.
Discuss: Have students share with one another before sharing with the whole class.
Remarks
- Working with loops can be tricky - especially working with the iterator variable. We will get more practice tomorrow by making an app that uses loops in an interesting way.
Assessment: Check for Understanding - AP Practice
For Students
Open a word doc or google doc and copy/paste the following question.
Question 1
What will be displayed after this code snippet is run?
a <- 0
REPEAT 3 TIMES
{
a <- a + 1
DISPLAY(a)
a <- a + 1
}
Question 2
What will be displayed after this code snippet is run?
count <- 0
REPEAT UNTIL (count = 3)
{
count <- count + 1
DISPLAY("and")
DISPLAY(count)
}
Standards Alignment
- CSTA K-12 Computer Science Standards (2017): 3A-AP-15, 3A-DA-12
- CSP2021: AAP-2.K.2, AAP-2.K.3, AAP-2.K.4, AAP-2.K.5
- CSP2021: AAP-2.L.1, AAP-2.L.2, AAP-2.L.5
- CSP2021: CRD-2.I.3, CRD-2.I.4
Next Tutorial
In the next tutorial, we will discuss Code.Org Unit 5 Lesson 8, which describes Make an app using lists and loops.