< Course F (2021)

Lesson 13: For Loop Fun

60 minutes

Overview

In this skill-building lesson, students will use loops with a powerful, new structure.

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!

CSTA K-12 Computer Science Standards (2017)
    • 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.

Agenda

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

  • Watch the *video.
  • Print one *worksheet per group.
  • Print one *assessment for each student.
  • Make sure every student has a reflection journal.

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).

Teaching Guide

Warm Up (20 minutes)

Vocabulary

This lesson has one new and important word:

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

Loops that have a predetermined start, stop, 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.

Main Activity (20 minutes)

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.

Teaching 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;
}

Directions:

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
Teaching Tip

If any of the values rolled are outside the bounds of the game (like rolling a start value of 6 but then rolling 2, 1, 2 for the end value), have the student re-roll everything.

Wrap Up (15 minutes)

Reflection

Teaching 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.

Flash Chat: What did we learn?

  • 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.

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 minutes)

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

Cross-Curricular Opportunity

Computer Science + Math

Standards Addressed:

  • MD - Measurement And Data

  • 2.MD.10: - Draw a picture graph and a bar graph (with single-unit scale) to represent a data set with up to four categories. Solve simple put- together, take-apart, and compare problems using information presented in a bar graph.

Creative Commons License (CC BY-NC-SA 4.0).

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.