Lesson 4.03: Nested For Loops

Learning Objectives

Students will be able to...

  • Define and identify: nested for loops, stack trace
  • Use nested for loops via a function and a for loop
  • Use nested for loops via two loops nested
  • Use a stack trace to understand and demonstrate the flow of nested for loops

Materials/Preparation

  • Do Now
  • Lab - Nested For Loops
  • Read through the Do Now, lesson, and lab so that you are familiar with the requirements and can assist students

Pacing Guide

Day 1

Duration Description
5 Minutes Do Now
10 Minutes Lesson
35 Minutes Lab
5 Minutes Debrief

Day 2

Duration Description
5 Minutes Do Now
10 Minutes Review
35 Minutes Lab
5 Minutes Debrief

Instructor's Notes

  1. Do Now
    • Display the Do Now on the board.
    • Students use nested for loops to create a square star pattern.
  2. Lesson
    • Go over part 1 of the Do Now.
      • Discuss the output of the program - were the students able to guess the output without typing it?
        • Go over how to read for loops if students are struggling (drawing the loop diagram). Make sure students are understanding loops and string concatenation.
        • If students continue to struggle, take 5 minutes to go over the loop syntax and practice.
    • Go over part 2 of the Do Now.
      • Ask students to write the print_star_squares function on the board.
      • Define nested for loop: a loop within another loop. For each iteration of the outer loop the inner loop is iterated through completely.
      • Draw a diagram (stack trace) of the for loop (e.g. something like loop diagram)
        • Ask students to draw the nested part of the state diagram (should be inside the outer loop but look the same as the outer loop)
    • Go over part 3 of the Do Now.
      • If students were unable to finish this, give them 5 minutes to practice in groups before calling them back to go over this part.
      • Ask students to write on the board how they did this. Ask them how treating the loop as it's own function made it easier or harder.
        • Ideally this should make it easier as a way of abstracting knowledge of looping.
  3. Lab
    • The lab asks students to write functions that produce different outputs using nested for loops.
  4. Debrief
    • Inform students that there will be a Unit 4 Quiz after Lesson 4.04.
    • Go over common questions the students had.
    • On the second day, if time allows, go over the bonus and discuss how students solved the problem.

Accommodation/Differentiation

If students need extra time for lab there is another day in the schedule for that.

This topic is often confusing for students new to the concept, so build in time for frequent individual checks for understanding.

The bonus problems for the lab are a bit harder and should allow students who are moving fast to work on something a bit trickier.