Lesson 24: For Loops: For Loop Fun

Overview

We know that loops allow us to do things over and over again, but now we’re going to learn how to use loops that have extra structures built right in. These new structures will allow students to create code that is more powerful and dynamic.

Purpose

At this point, students have become masters of loops. Today, they will learn about another loop commonly used in programming. The for loop repeats commands a certain number of times, but also keeps track of the values it is iterating over. For example, a for loop that begins at 4, ends with 8, and has a step value of 1 will repeat 4 times, but the values 4, 5, 6, and 7 will also be captured for use elsewhere. Using this structure with variables can create some pretty fantastic programs. Today, students will simply be learning the basics of a for loop before diving into programming with them next time!

Agenda

Warm Up (20 min)

Main Activity (20 min)

Wrap Up (15 min)

Assessment (5 min)

Extended Learning

View on Code Studio

Objectives

Students will be able to:

  • Determine starting value, stopping value, and stepping value for a `for` loop.
  • Illustrate the counter values hit each time through a for loop during runtime.

Preparation

Links

Heads Up! Please make a copy of any documents you plan to share with students.

For the Teachers

For the Students

Vocabulary

  • For Loop - Loops that have a predetermined beginning, end, and increment (step interval).

Support

Report a Bug

Teaching Guide

Warm Up (20 min)

Vocabulary

This lesson has one new and important word:

  • For Loop - Say it with me: For-Loop

Loops that have a predetermined beginning, end, and step value.

For One and All

  • Point out that there are certain loops that happen very frequently, for example, loops where you need to keep track of how many times you have been through
    • Sometimes, you don't want to start with one
    • Sometimes, you don't want to count by ones
    • for loops give you a powerful way to keep a counter that starts when you want, ends when you want, and increases by whatever size step that you want

Here, you can jump right into a sample of the game (example in English)

Main Activity (20 min)

For Loop Fun - Worksheet

Sometimes we want to repeat things a certain number of times, but we want to keep track of values as we do. This is where a for loop comes in handy. When you use a for loop, you know right from the start what your beginning value is, what your ending value is, and how much the value changes each time through the loop.

for Loop block (in English)

Directions:

Lesson Tip

When you play this game, it's as if you're running through a loop like this

for (x=startValue; x <= stopValue; x = x + step){
circle currentXvalue;
add currentXvalue to roundScore;
}

It may be difficult for young students to understand this written in pseudocode, but it may be helpful to have you explain out loud (and perhaps with a diagram) what they will be using as the content of a for loop.

  • Divide students into pairs
  • To start the round, each student rolls three times:
    • One die to determine the starting value of X
    • Three dice to determine the stopping value for X
    • One die to determine the stepping value of X each time through
  • Use one of the provided number lines to trace the for loop that they’ve made
    • Start at the starting value of X
    • Count down the number line, circling the numbers at the rolled interval
    • Stop when you get to the predetermined stopping value
  • Add all of the circled values to your score, then let the other player take a turn
  • Best 2 out of 3 wins

Wrap Up (15 min)

Flash Chat: What did we learn?

Lesson Tip

Flash Chat questions are intended to spark big-picture thinking about how the lesson relates to the greater world and the students' greater future. Use your knowledge of your classroom to decide if you want to discuss these as a class, in groups, or with an elbow-partner.

  • What would your interval need to be if you wanted to count from 4 to 13 by threes?
  • What kinds of things do you think you could do with a for loop?
  • Can you reproduce a normal loop using a for loop?
  • What would you need to do?

Journaling

Having students write about what they learned, why it’s useful, and how they feel about it can help solidify any knowledge they obtained today and build a review sheet for them to look to in the future.

Journal Prompts:

  • What was today's lesson about?
  • How do you feel about today's lesson?
  • What is a for loop?
  • Why would you use a for loop instead of a repeat loop or a while loop?

Assessment (5 min)

For Loop Fun - Assessment

Hand out the assessment worksheet and allow students to complete the activity independently after the instructions have been well explained. This should feel familiar, thanks to the previous activities.

Extended Learning

Use these activities to enhance student learning. They can be used as outside of class activities or other enrichment.

Run it Backward

  • Try this activity again, but this time have the start number be selected using three dice, and the stop number with only one. Make sure to have a negative increment!

Hop Scotch

  • Using chalk, draw a hop scotch diagram outside on the blacktop
    • Number the squares from bottom to top
    • Have students give each other a start square, stop square, and how many at a time they need to jump
    • When the jumper is done, have them write down the loop they just performed
    • Start adding additional activities to be done at each square, this will add complexity to the written portion, as well

Standards Alignment

View full course alignment

CSTA K-12 Computer Science Standards (2017)

AP - Algorithms & Programming
  • 1B-AP-09 - Create programs that use variables to store and modify data.
  • 1B-AP-11 - Decompose (break down) problems into smaller, manageable subproblems to facilitate the program development process.