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.

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.

Heads Up! Please make a copy of any documents you plan to share with 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.

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

"This is going to BEE great!"

Help the bee collect all of the nectar.

View on Code Studio

Student Instructions

"This is going to BEE great!"

Help the bee collect all of the nectar.

View on Code Studio

Student Instructions

View on Code Studio

Student Instructions

Look closely at the nested loops below. What will happen when you click "Run"?

The bee will move forward and get nectar only one time.

The bee will get only 2 units of nectar.

The bee will get all of the nectar.

I don't know.

View on Code Studio

Student Instructions

This time, help the bee collect all of the nectar using as few blocks as possible.

View on Code Studio

Student Instructions

"Zombie hungry!"

Get the zombie to the sunflower using the fewest number of blocks possible.

View on Code Studio

Student Instructions

Get the zombie to the sunflower using the fewest blocks possible!

View on Code Studio

Student Instructions

View on Code Studio

Student Instructions

"Must eat sunflower!"

Get the zombie to the sunflower using only the blocks available.

View on Code Studio

Student Instructions

Challenge: Figure out how to get all of the nectar using only the blocks available.

View on Code Studio

Student Instructions

Collect all of the nectar from each flower and make honey at the honeycomb.

View on Code Studio

Student Instructions

Make all of the honey.

View on Code Studio

Student Instructions

Look closely at the code below. How much honey will the bee make when you click "Run"?

4

8

12

I don't know.

View on Code Studio

Student Instructions

"I need some help on the farm! Help me harvest this corn using only the blocks in your toolbox."

View on Code Studio

Student Instructions

"Let's hunt for treasure - using loops!"

Help the collector reach at least two piles of treasure using only the blocks in your toolbox. It is possible to collect all 4.

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