Students practice using the for loop in order to repeatedly run pieces of code. The lesson begins with a quick investigation of an app that flips coins. After that code investigation students complete another investigation with an app that uses loops to update screen elements.
Students will be able to:
This lesson is students primary opportunity to get hands on with loops in code prior to the Practice 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.
Prompt: Imagine you are interested in finding out how much time it takes on average to walk from one end of your school to the next. You've decided to figure this out on your lunch break, and are able to complete the walk 20 times. What would your algorithm look like? Where could a loop show up? (Note: You do not need to write your algorithm in a programming language. You can write it out in English or in pseudocode.)
Discuss: Compare your algorithms with a partner and then discuss as a class.
The point of this activity is for students to see the need for a loop. Algorithms might look something like this:
Prompt: The first app we are going to investigate uses a loop in a simulation. What is a simulation? Why are they useful?
Simulations allow us to test solutions or run experiments in a way that is usually cheaper, safer, and often times faster. Simulations are models of the real world that allow the user to investigate hypotheses and draw conclusions. Oftentimes they involve inputting various sets of data or values to demonstrate the changing state of a phenomenon.
For this lesson, there are two separate investigations of two different apps. Lead the class through discussions, and circulate around the classroom as students work.
As time allows, encourage students to do the Modify tasks for each level. This will give students extra practice.
Group: Place students in pairs.
Open a Project: Students will investigate two apps for today's lesson. Direct students to begin with Lesson6_App1 in the Unit5 folder of the CSP-Widgets repository.
Project 1, Part 1 - Coin Flipper App (App Investigation): This code investigation includes a number of steps to help students get familiar with a new app. Students run the app and discuss the following:
Project 1, Part 2 - Coin Flipper App (Code Investigation): Students carefully read each individual part of the program before discussing the following:
Prompt: How does a loop help when running similar simulations?
A loop lets us repeat code quickly to determine an answer to a question.
Inside the loop we can use random number generation to get a variety of possible answers, similar to what would happen in the real world.
Project 2 - Label Tester App: Students open the second project and run the app before discussing the following:
"Output " + iused? How does it evaluate with each round of the loop?
Output 1, etc. to
Label 1, etc.
i < 4in the loop?
i < 3
i < 2
i <= 3
Prompt: What aspects of working with loops do you feel like clicked today? What do you still feel like you have trouble with?
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.
Question: Think back on the Label Tester app. Can you think of an example of another app or feature of an app which would use a loop to control different elements on a screen?