Lists, Loops, and Traversals - Lesson 6: Loops Investigate

Overview

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.

Goals

Students will be able to:

  • Read programs that use loops
  • Understand the parts of a loop
  • Update the Boolean expression in a for loop to change how many times the loop runs

Purpose

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.

Resources

Getting Started (5 minutes)

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.

Discussion Goal

The point of this activity is for students to see the need for a loop. Algorithms might look something like this:

  • Repeat 20 times:
    • Stand at the entry doors to the school.
    • Start the timer.
    • Walk at a steady pace to the end of the hallway.
    • Turn off the timer.
    • Record the time in a notebook.
    • Walk back to the entry doors.
  • Add all times together.
  • Divide by 20 to find the average.

Remarks

  • A loop helps simplify code. Let's investigate how this looks in two different apps.

Activity (35 minutes)

Teaching Tip

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.

Prompt: The first app we are going to investigate uses a loop in a simulation. What is a simulation? Why are they useful?

Discussion Goal

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.

Remarks

  • Thanks for sharing your thoughts on simulations! Other examples of simulations are flight simulators which allows you to practice flying without damaging yourself or a real plane or a city building game where you can play with tweaking multiple values in the economy without real consequences.
  • These examples demonstrate the process of developing an abstract simulation where the specific details have been removed, or the functionality simplified.
  • While simulations are a useful way to model the real-world, we also need to be constantly on the lookout for bias in the real-world elements that are actually included in the simulation.
  • Today, we are going to look at a simple simulation.
  • Simulations can be helpful to run when it would take a long time or it would be too impractical to do something physically. For example, what if we wanted to see the results of a coin flip that we flipped a million times? That would take a long time to perform ourselves, but a computer can run that simulation much quicker. Let's look at an app that does just that.

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:

  • What information does the user input?
  • How does the app process that information?
    • What is being repeated?
    • Where is there an opportunity to use a loop?
  • What information does the app output?

Project 1, Part 2 - Coin Flipper App (Code Investigation): Students carefully read each individual part of the program before discussing the following:

  • On what lines of code is the program using a loop?
  • Which lines of code decide how many times to flip the coin?
  • What does the ++ command seem to do?

Prompt: How does a loop help when running similar simulations?

Discussion Goal

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.

  • Now let's investigate an app that uses loops in a different way. This time loops are used to update elements on the screen.

Level 3 Project 2 - Label Tester App: Students open the second project and run the app before discussing the following:

  • Find the four different for loops in the program. What do they each do?
  • Also take a look at the names of the screen elements in Design Mode. Then navigate back to the code. How is "text " + i used? How does it evaluate with each round of the loop?
Discussion Goal

This is a way of doing something to a set of elements. In this case, we have five text boxes namedtext0,text1,text2,text3,text4

  • text + i is evaluated each round of the loop.
    • First Round: text0
    • Second Round: text1
    • Third Round: text2
    • Fourth Round: text3
    • Fifth Round: text4
  • So when the first round of the loop is executed, the element with the text0 ID is changed to whatever information is in the for loop.

Do This

  • What happens if you change the label variable names?
  • What happens if you change the label variable names? Try changing them from text0, text1, etc. to textbox0, textbox1, etc.
  • What do you need to change in the code for the program to still work?

Do This

  • What happens if you change the Boolean expression i < 5 in the loop?
  • Change all the Boolean expressions to one of the options below, run the program, and discuss. Then move to the next option and repeat:
    • i < 4
    • i < 3
    • i <= 4

Wrap Up (5 minutes)

Remarks

  • Let's review the parts of a loop.

Prompt: What aspects of working with loops do you feel like clicked today? What do you still feel like you have trouble with?

Discuss: Have students share with one another before sharing with the whole class.

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.

  • Working with loops can be a little tricky. We'll have more opportunities to practice combining loops and lists together to make even more powerful apps.

Assessment: Check for Understanding

For Students

Open a word doc or google doc and copy/paste the following question.

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?

Standards Alignment

  • CSTA K-12 Computer Science Standards (2017): 3A-AP-15, 3A-AP-23, 3A-DA-12
  • CSP2021: AAP-3.F.1, AAP-3.F.2, AAP-3.F.3, AAP-3.F.4, AAP-3.F.5, AAP-3.F.6, AAP-3.F.7, AAP-3.F.8

Next Tutorial

In the next tutorial, we will discuss Code.Org Unit 5 Lesson 7, which describes Practice with loops.