Lesson 11: 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 puzzles to find any potential problem areas for your class.
- Make sure every student has a journal.
Vocabulary
- 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.
Student Instructions
Student Instructions
Now loop the triangle 6 times.
After each triangle, you'll need to turn 60 degrees before drawing the next.
- Video
- 3
Student Instructions
This time, complete the puzzle with the fewest number of blocks possible.
After each triangle, you'll need to turn 60 degrees before drawing the next. See how much easier this is with nested loops?
Student Instructions
Student Instructions
Great! Do the same thing with these circles.
- Each circle is made by moving 1 pixel before turning 1 degree, 360 times.
- Each circle begins just 50 pixels from where the last one ended
Student Instructions
What happens if you also turn 90 degrees between circles?
(To get this image, you still need to jump 50 pixels between circles)
Student Instructions
Use what you've learned to make this drawing.
- The squares each have 100 pixel sides and 90 degree angles
- You will need to turn 60 degrees between each square. Why? Because there are 6 squares, and 360 degrees (a full turn around) divided by 6 is 60 degrees.
- Make sure you jump 50 pixels to get to the next square
Student Instructions
Using what you have learned in the last couple of puzzles, build this image from the beginning.
- Each hexagon has 50 pixel sides and 60 degree turns
- Challenge
- 10
Student Instructions
Challenge: Can you figure out how to make a picture like this?
- Both shapes have 50 pixel sides
- Between each pair of shapes, you will need to jump forward the length of a side, and turn 45 degrees.
- Practice
- 11
Student Instructions
- Prediction
- 12
Student Instructions
A | B | C |
Take a good look at the code below. Which drawing will this program make when you click "Run"?
A
B
C
I don't know.
- Free Play
- 13
Student Instructions
Now it's your turn. Take the skills you have learned and make something that you love!
Need an idea? Try to make one of these:
Student Instructions
Can you figure out how to make a picture like this?
- Both shapes have 50 pixel sides.
- The octagons are made with 45 degree turns.
- The triangles are made with 120 degree turns.
Student Instructions
This code is just a little bit off! Can you fix it to make the correct drawing in the workspace?
- The first inner loop should make an octagon.
- The second inner loop should make a hexagon.
- The third inner loop should make a square.
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.
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
- 4.L.6 - Acquire and use accurately grade-appropriate general academic and domain-specific words and phrases, including those that signal precise actions, emotions, or states of being (e.g., quizzed, whined, stammered) and that are basic to a particular topic (e.g
SL - Speaking & Listening
- 4.SL.1 - Engage effectively in a range of collaborative discussions (one-on-one, in groups, and teacher-led) with diverse partners on grade 4 topics and texts, building on others’ ideas and expressing their own clearly.
- 4.SL.1.b - Follow agreed-upon rules for discussions and carry out assigned roles.
- 4.SL.4 - Report on a topic or text, tell a story, or recount an experience in an organized manner, using appropriate facts and relevant, descriptive details to support main ideas or themes; speak clearly at an understandable pace.
- 4.SL.6 - Differentiate between contexts that call for formal English (e.g., presenting ideas) and situations where informal discourse is appropriate (e.g., small-group discussion); use formal English when appropriate to task and situation.
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.3 - Construct viable arguments and critique the reasoning of others
- 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
- 4.OA.1 - Interpret a multiplication equation as a comparison, e.g., interpret 35 = 5 × 7 as a statement that 35 is 5 times as many as 7 and 7 times as many as 5. Represent verbal statements of multiplicative comparisons as multiplication equations.
- 4.OA.5 - Generate a number or shape pattern that follows a given rule. Identify apparent features of the pattern that were not explicit in the rule itself. For example, given the rule “Add 3” and the starting number 1, generate terms in the resulting sequence and
Next Generation Science Standards
ETS - Engineering in the Sciences
ETS1 - Engineering Design
- 3-5-ETS1-1 - Define a simple design problem reflecting a need or a want that includes specified criteria for success and constraints on materials, time, or cost.
- 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.
- 3-5-ETS1-3 - Plan and carry out fair tests in which variables are controlled and failure points are considered to identify aspects of a model or prototype that can be improved.