Lists, Loops, and Traversals - Lesson 5: Loops Explore
Overview
Students begin the lesson by discussing the purpose of loops before completing the unplugged activity. This activity involves moving a "robot" around a game board while practicing tracing blocks of code by hand. To conclude, the lesson is wrapped up with a vocabulary discussion and a video.
Goals
Students will be able to:
- Use appropriate vocabulary to describe loops.
- Identify the exit point of a loop.
- Trace a simple program with a loop.
Purpose
Students are introduced to the concept of loops through a robot maze activity. This unplugged lessons provides students a physical mental model they will be able to use when they start programming with loops in the subsequent lessons.
Resources
For the students
Supplemental code.org material
Preparation
- 1 game board per pair of students
- 1 "robot" per pair of students
- Game pieces, markers, or tokens that can be used to represent barriers
- Review the optional Intro to Loops presentation and click through all of the animations if you plan on using the slides
Teaching Tip
- Robot: The "robot" can be any small item that fits in a square on the game board and is clearly facing a direction. It could be as simple as a scrap of paper shaped like a triangle or a paperclip with googly eyes.
- Game Pieces: The game pieces can be anything that will fit in a square on the game board that is clearly distinguishable from the robot. For example, you can use knick-knacks, tokens, markers, paperclips, or scraps of paper.
- Game Board: The game board should be a grid that is 5x5. You can use a grid printed on paper, a chess board, or a tactile grid, such as a LEGO board. If your grid is larger than 5x5, you should mark off the excess space. The robot and obstacles should only be placed within the 5x5 section for this activity.
Getting Started (5 minutes)
Set-up
Distribute: The activity today involves a lot of moving pieces. Take this time at the beginning of class to pass out supplies and pair students.
Per pair of students:
- 1 game board
- 1 "robot"
- At least 4 game pieces, markers, or tokens that can be used to represent barriers
Group: Put students in groups of two.
Activity (30 minutes)
Loops
Teaching Tip
This activity asks students to follow along as a number of core concepts for programming are introduced. The model is typically that a term or concept is introduced and modeled and then afterwards students are encouraged to try it out on their own. Trying it out typically means they are writing information on a note and sharing it with another group before discussing the results with the whole class.
Slides with animations have an icon in the bottom left corner to let you know you need to click to reveal more of the slide's content.
To help you more easily prepare the activity and keep track of your instructions, detailed instructions have been included as speaker notes in the presentation. Here are some tips to help you throughout the presentation.
- There are opportunities throughout the presentation for students to actively engage. At these moments students should be making things with their manipulatives or using them to answer questions. Use these opportunities to check progress.
- There is a fair amount of new vocabulary introduced but it is introduced gradually and with intentional repetition. Make a point of actively modeling the use of new terms.
- The most important goal here is building a mental model. It is ok if students have some open questions that will get resolved over the subsequent conditional lessons.
- Both you and students can use the "Key Takeaways" to check your understanding at the end.
Guided Activity: Today's activity introduces students to the concept of loops. As a visual aid, you can use Code.org's presentation slides for Unit 5, Lesson 5: Loops Explore. These slides include animations. The notes below describe when to move to the next slide or click through an animation -- if you aren't using the slides, you can ignore these prompts.
Guided Activity
Follow this lesson with the supplemental slides.
Slide: Do this: You have one command
Say: Today we are going to explore Loops. You will move a robot around a game board following commands. Here's the first one: MoveForward()
Do This: Place the robot in the center of the bottom row, facing the top row. Use the MoveForward()
command to write a program to move the robot to the top row.
Click through animation to reveal the answer.
Slide: This code is repetitive.
Say: This code is repetitive. How can we simplify it? By using a While Loop.
Slide: Here's how it works:
Say: Here's how a While Loop works. While something is true, the code in the while loop runs, over and over until that thing is no longer true. Let's try it together!
Slide: var steps = 0;
Say: Let's run this program together. Set up your board with the robot in the center of the bottom row, facing the top. We are going to run each step of the loop together. First you will try it on your board, and then we will look at the answers together.
integer steps = 0
repeat while steps < 4
MoveForward()
steps = steps + 1
end
Slide: Round 1
Say: Read the code with your partner and complete the first round of the loop. You may want to keep track of the value in the variable using a note or scrap of paper.
Click through for animation to view the answer after students have had a chance to follow the code on their own board. Discuss each part of the code as it is running.
Slide: Round 2
Say: Let's continue on with the second round. You do it first.
Click through for animation to view the answer after students have had a chance to follow the code on their own board. Discuss each part of the code as it is running.
Slide: Round 3
Say: Now on to the third round. You do it first.
Click through for animation to view the answer after students have had a chance to follow the code on their own board. Discuss each part of the code as it is running.
Slide: Round 4
Say: Time for the fourth round. You do it first.
Click through for animation to view the answer after students have had a chance to follow the code on their own board. Discuss each part of the code as it is running.
Slide: Round 5
Say: What happens here? Discuss with your partner.
Click through for animation to view the answer after students have had a chance to follow the code on their own board. Discuss each part of the code as it is running. The main thing to point out here is that the loop ends because the Boolean expression evaluates to false.
Slide: var steps = 0;
Do This: Put the robot on the bottom row in the center column, facing upwards. With your partner, run this program on your board. What happens?
integer steps = 0
repeat while steps < 4
MoveForward()
end
Click for animation.
Discuss: Why does the robot run off the board?
This is an example of an infinite loop. The program never ends because steps will always be less than 4.
Slide: Do This: You have a new command.
Say: You have a new command, TurnRight()
Do This: Put the robot on the bottom row in the second column, facing upwards. Run this program on your board. Where will the robot end up?
integer steps = 0
repeat while steps < 4
MoveForward()
TurnRight()
steps = steps + 1
end
Click through animation to see the answer. The robot returns to its original position.
Slide: While Loop: 3 Parts
Say: A While Loop has three distinct parts.
- A counting variable set to an initial value
- A Boolean expression which checks the condition of that variable
- A statement which increases or decreases the variable that is being checked
- Note: if this piece is missing, you may create an infinite loop that never stops running!
Click for animation: Read each step as the animation highlights the program code.
Slide: A for loop combines these three parts into one statement
Say: A For Loop combines all three of these parts into one statement.
Click for animation: Read each step as the animation highlights the Javascript code, or look at the Quorum code below.
repeat 4 times
MoveForward()
end
Note: The "repeat times" loop in Quorum is slightly different than the traditional "for" loop in Javascript. Both loops make it possible to iterate a specific number of times, but in Quorum, you don't make a variable as part of the loop declaration.
Say: A loop is an example of iteration: a repetitive portion of an algorithm which repeats a specified number of times or until a given condition is met.
Slide: Do this: You have another new command
Say: You have another new command: TurnLeft()
Click to reveal the program code, or share the Quorum code below. Have students place the robot in the bottom left corner of their grid, facing forward again.
repeat 3 times
MoveForward()
TurnRight()
MoveForward()
TurnLeft()
end
Do This: With your partner, follow the code with your robot.
Click through the animation to see the answer.
Slide: Do this: click to see a new program.
Click to reveal the program code, or share the Quorum code below.
integer steps = 0
repeat while steps <= 2
MoveForward()
MoveForward()
TurnRight()
MoveForward()
steps = steps + 1
end
Do This: Place the robot on the second row from the bottom, in the left-most column, facing towards the top. Run the program on your board. Where will the robot end up?
Click through the animation to see the answer.
Slide: Do this: Click to see a new program.
Click to reveal the program code, or share the Quorum code below.
repeat 2 times
MoveForward()
TurnLeft()
TurnLeft()
MoveForward()
MoveForward()
end
Do This: Place the robot on the center of your board, then run the program. Where will the robot end up?
Click through the animation to see the answer
Slide: Let's make it a little more challenging.
Say: We're going to add some new commands to make things more challenging: CanMoveLeft()
, CanMoveRight()
, CanMoveForward()
, and CanMoveBackward()
. These evaluate to true or false and are dependent on the direction that the robot is facing. If a barrier or a wall (the edge of the board) is in the way, the Boolean expression evaluates to False.
Slide: canMove(forward);
Click through the animation to see the answer.
Say: In this example, we have our robot in the center of our grid, facing upwards. There is a barrier in the space immediately to its left. CanMoveLeft()
returns false, and the other commands return true.
Slide: canMove(forward);
Click through the animation to see the answer.
Say: In this example, our robot is in the top row, in the center column, and facing to the right. There is a barrier immediately behind it, so CanMoveBackward()
returns false. CanMoveLeft()
also returns false, because the robot's left side is up against a wall (the edge of the board).
Slide: Do This: Run the following program.
Click to reveal the program code, or share the Quorum code below.
repeat 3 times
if CanMoveRight()
TurnRight()
MoveForward()
end
TurnLeft()
if CanMoveForward()
MoveForward()
end
end
Do This: Place the robot in the center of the grid facing upwards. Then place a barrier one space below the robot, and another barrier one space above and to the right of the robot. Then run the program on your board. Where will the robot end up?
Click through the animation to see the answer.
Slide: Do this: Run the following program.
Click to reveal the program code, or share the Quorum code below.
repeat 4 times
if CanMoveForward()
MoveForward()
end
TurnLeft()
if CanMoveForward()
MoveForward()
end
Turn Right()
end
Do This: Place the robot in the fourth column and the bottom row, facing upwards. Then, place a barrier one space to the left of the robot, and another barrier three spaces to the left of the robot. Finally, place a barrier in the second column in the top row and another in the second column in the center row. Then run the program on your board. Where will the robot end up?
Click through the animation to see the answer.
Slide: Challenge!
Say: Now it's your turn to write your own programs!
Do This: With a partner, set up a game board and add as many barriers as you'd like. Write a program using a for loop to navigate the board. Figure out the starting and ending points of the robot. Share your board and code with another group. See if you agree on the ending point of the robot!
Slide: Key Takeaways
Do This: Review the key takeaways with students.
Note: The key takeaways are slightly different between Javascript and Quorum because the "for" and "repeat times" loops are not identical. One important takeaway for both languages though is that the "while" loop can do anything the other loops can. The other loop styles are just for convenience.
- Repeat While Loops use a boolean condition to repeatedly run a block of code. If it is true it runs the block of code contained within it. This process of checking the condition and running the block of code is repeated as long as the Boolean condition remains true. Once the Boolean expression becomes false it will stop.
- Repeat Times Loops explicitly run a set number of times. How many times this kind of loop runs could be a constant number or it could be a variable.
Wrap Up (10 minutes)
Introduction to Loops
Video: As a class, watch the CS Principles: Using Loops video from Code.org. The video discusses the use of loops in Javascript, but most of the concepts are still useful in Quorum as well.
Journal
Have students add the following terms to their journals:
- iteration: a repetitive portion of an algorithm which repeats a specified number of times or until a given condition is met.
- infinite loop: occurs when the ending condition will never evaluate to true.
Assessment: Check for Understanding
For Students
Open a word doc or google doc and copy/paste the following question.
Question
When breaking a problem down, you often encounter elements that you want to use repeatedly in your code. Sometimes it's appropriate to write a new function; at other times it's appropriate to write a loop.
Reminder: There is no hard-and-fast rule as to which is better, but what do you think? What kinds of circumstances would lead you to writing a function versus using a loop?
Standards Alignment
- CSTA K-12 Computer Science Standards (2017): 3A-AP-15
- CSP2021: AAP-2.K.1, AAP-2.K.4
Next Tutorial
In the next tutorial, we will discuss Code.Org Unit 5 Lesson 6, which describes Investigate how loops are used.