Lesson 8: Nested Loops in Maze

Overview

In this online activity, students will have the opportunity to push their understanding of loops to a whole new level. Playing with the Bee and Plants vs Zombies, students will learn how to program a loop to be inside of another loop. They will also be encouraged to figure out how little changes in either loop will affect their program when they click Run.

Purpose

In this introduction to nested loops, students will go outside of their comfort zone to create more efficient solutions to puzzles.

In earlier puzzles, loops pushed students to recognize repetition. Here, students will learn to recognize patterns within repeated patterns to develop these nested loops. This stage starts off by encouraging students try to solve a puzzle where the code is irritating and complex to write out the long way. After a video introduces nested loops, students are shown an example and asked to predict what will happen when a loop is put inside of another loop. This progression leads into plenty of practice for students to solidify and build on their understanding of looping in programming.

Agenda

Warm Up (10 min)

Main Activity (30 min)

Wrap Up (15 min)

View on Code Studio

Objectives

Students will be able to:

  • Break complex tasks into smaller repeatable sections.
  • Recognize large repeated patterns as made from smaller repeated patterns.
  • Identify the benefits of using a loop structure instead of manual repetition.

Preparation

  • Play through the lesson to find any potential problem areas for your class.
  • Make sure every student has a journal.

Links

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

For the Students

Vocabulary

  • Command - An instruction for the computer. Many commands put together make up algorithms and computer programs.
  • Loop - The action of doing something over and over again.
  • Repeat - To do something again.

Support

Report a Bug

Teaching Guide

Warm Up (10 min)

Introduction

Briefly review with the class what loops are and why we use them.

  • What do loops do?
    • Loops repeat a set of commands. (see vocabulary on command if students don't recognize it)
  • How do we use loops?
    • We use loops to create a pattern made of repeated actions.

Tell the class that they will now be doing something super cool: using loops inside loops. Ask the class to predict what kinds of things we would be using a loop inside of a loop for.

"If a loop repeats a pattern, then looping a loop would repeat a pattern of patterns!"

Students don't need to understand this right away, so feel free to move on to the online puzzles even if students still seem a little confused.

Main Activity (30 min)

Online Puzzles

We highly recommend Pair Programming - Student Video in this lesson. This may not be an easy topic for the majority of your students. Working with a partner and discussing potential solutions to the puzzles might ease the students' minds.

Also, have paper and pencils nearby for students to write out their plan before coding. Some puzzles have a limit on the number of certain blocks you can use, so if students like to write out the long answer to find the repeats, paper can be useful.

Wrap Up (15 min)

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 did you feel about today's lesson?
  • What is a nested loop?
  • Can you draw a puzzle that would use a nested loop? Try coding the solution to your own puzzle.
View on Code Studio

Student Instructions

The artist is in the wrong spot!

Use the jump to 200 over 200 down block to get the artist to the center of the play area.
Then draw a line 50 pixels long with the move forward block.

View on Code Studio

Student Instructions

Now, put this code into a repeat block and loop it 6 times. - Notice that we have added a 60 degree turn at the end of the looped code to get a full rotation.

View on Code Studio

Student Instructions

"Fantastic!"

Let's add some style by changing the brush using the set pattern block before our loop starts.

View on Code Studio

Student Instructions

Free Play: Let's build something bigger, one step at a time!

Get the artist to the center of the play area using the jump to the middle center position block, then use the draw sticker block to make something appear.


Press "Finish" when you are happy with what you've made.

expandable expandable expandable

View on Code Studio

Student Instructions

Now, use a loop to make more copies of your sticker. Try changing the numbers in the turn and repeat blocks to make different patterns.


Press "Finish" when you are happy with what you've made.

expandable expandable expandable

View on Code Studio

Student Instructions

So much fun!

Now, add a second sticker right on top of the first one to make an image like the ones below.


Press "Finish" when you are happy with what you've made.

expandable expandable expandable

View on Code Studio

Student Instructions

Edit your code so that you jump forward between drawing the two stickers. In this puzzle, make sure you have jump to the middle center position inside of your loop, so you stay in the center!


Press "Finish" when you are happy with what you've made.

expandable expandable expandable expandable

View on Code Studio

Student Instructions

What happens when you replace one or both of the stickers with a different creature?


expandable

View on Code Studio

Student Instructions

What happens when you move forward then move backward by 100, 200, or 400 pixels somewhere inside the loop?
- Try using the set pattern block before you start your loop to make a work of art!


expandable expandable expandable

View on Code Studio

Student Instructions

Add a third sticker anywhere in the loop that you want! What does it look like now? - Change the pattern on your brush for more variety!


expandable

View on Code Studio

Student Instructions

Try changing the repeat loop to run a different number of times. How far do you have to turn to go around in a full circle? - Try changing how many times you spin - Try a 60 degree turn for 6 times around - Try a 20 degree turn for 18 times around


This image uses a pirate, a ninja, and a giraffe. They loop 18 times with a 20 degree turn.

expandable

View on Code Studio

Student Instructions

Now make something on your own. What happens when you make two separate circles of different stickers?


Need an idea? Try making a design like this one.

expandable

Standards Alignment

View full course alignment

CSTA K-12 Computer Science Standards (2017)

AP - Algorithms & Programming
  • 1B-AP-11 - Decompose (break down) problems into smaller, manageable subproblems to facilitate the program development process.
  • 1B-AP-12 - Modify, remix or incorporate portions of an existing program into one's own work, to develop something new or add more advanced features.
  • 1B-AP-15 - Test and debug (identify and fix errors) a program or algorithm to ensure it runs as intended.

Cross-curricular Opportunities

This list represents opportunities in this lesson to support standards in other content areas.

Common Core English Language Arts Standards

L - Language
  • 3.L.6 - Acquire and use accurately grade-appropriate conversational, general academic, and domain-specific words and phrases, including those that signal spatial and temporal relationships (e.g., After dinner that night we went looking for them).
SL - Speaking & Listening
  • 3.SL.1 - Engage effectively in a range of collaborative discussions (one-on-one, in groups, and teacher-led) with diverse partners on grade 3 topics and texts, building on others’ ideas and expressing their own clearly.
  • 3.SL.1.b - Follow agreed-upon rules for discussions (e.g., gaining the floor in respectful ways, listening to others with care, speaking one at a time about the topics and texts under discussion).
  • 3.SL.6 - Speak in complete sentences when appropriate to task and situation in order to provide requested detail or clarification.

Common Core Math Standards

MP - Math Practices
  • MP.1 - Make sense of problems and persevere in solving them
  • MP.2 - Reason abstractly and quantitatively
  • MP.5 - Use appropriate tools strategically
  • MP.6 - Attend to precision
  • MP.7 - Look for and make use of structure
  • MP.8 - Look for and express regularity in repeated reasoning
OA - Operations And Algebraic Thinking
  • 3.OA.4 - Determine the unknown whole number in a multiplication or division equation relating three whole numbers. For example, determine the unknown number that makes the equation true in each of the equations 8 × ? = 48, 5 = � ÷ 3, 6 × 6 = ?.

Next Generation Science Standards

ETS - Engineering in the Sciences
ETS1 - Engineering Design
  • 3-5-ETS1-2 - Generate and compare multiple possible solutions to a problem based on how well each is likely to meet the criteria and constraints of the problem.