Lesson 17: While Loops in Farmer
Overview
By the time students reach this lesson, they should already have plenty of practice using repeat
loops, so now it's time to mix things up.
While loops are loops that continue to repeat commands while a condition is met. While loops are used when the programmer doesn't know the exact number of times commands need to be repeated, but does know what condition needs to be true in order for the loop to continue repeating. For example, students will be working to fill holes and dig dirt in Farmer. They will not know the size of the holes or the height of the mountains of dirt, but the students will know they need to keep filling the holes and digging the dirt as long as the ground is not flat.
Purpose
As your students continue to deepen their knowledge of loops, they will come across problems where a command needs to be repeated, but it is unknown how many times it needs to be repeated. This is where while loops come in. In today's lesson, students will develop a beginner's understanding of condition-based loops and also expand their knowledge of loops in general.
Agenda
Warm Up (10 min)
Bridging Activity - Conditionals (15 min)
Main Activity (30 min)
Wrap Up (15 min)
Extended Learning
View on Code Studio
Objectives
Students will be able to:
- Distinguish between loops that repeat a fixed number of times and loops that repeat as long as a condition is true.
- Use a while loop to create programs that can solve problems with unknown values.
Preparation
- Play through CSF Express Course - Website to find any potential problem areas for your class.
- Review CS Fundamentals Main Activity Tips - Lesson Recommendations.
- Make sure every student has a Think Spot Journal - Reflection Journal.
Links
Heads Up! Please make a copy of any documents you plan to share with students.
For the Teachers
- CSF Express Course - Website
- CS Fundamentals Main Activity Tips - Lesson Recommendations
For the Students
- Unplugged Blocks (Courses C-F) - Manipulatives
- Think Spot Journal - Reflection Journal
Vocabulary
- Condition - Something a program checks to see if it is true before allowing an action.
- Loop - The action of doing something over and over again.
- Repeat - To do something again.
- While Loop - A loop that continues to repeat while a condition is true.
Support
Report a Bug
Teaching Guide
Warm Up (10 min)
Introduction
Use "while" in a sentence in front of the students. Ask the students what the word "while" means. If you were to say "while there is a hole, fill it with dirt" what would they do? How long would they do that?
When you use a word like “while”, you are relying on a condition to tell the computer how long the loop should run. A condition is a statement that is tested and found to be true or false. In the case above, the condition is if there is a hole. It’s only possible for there to be a hole or for there not to be a hole, thus the statement is only ever true or false.
Tell the students they will be learning about a new kind of loop. Previously, students only used loops to repeat a command a certain number of times. Here, they won't always know how many times to repeat the command, however, they will know when to stop or when to keep going. While loops allow the programmer to repeat a command as long as a condition is still true. In the previous example, the condition is the existence of a hole.
If there's time, have the students discuss other times using a while loop would be useful. Examples include:
- Running toward a ball while it is in front of you.
- Filling a glass while it has space for more liquid.
- Walk forward while there is a path ahead.
Bridging Activity - Conditionals (15 min)
This activity will help bring the unplugged concepts from "Conditionals With Cards" into the online world that the students are moving into. Choose one of the following to do with your class:
Unplugged Activity Using Paper Blocks
Print and cut out 2-3 if / else
and blank action blocks from Unplugged Blocks (Courses C-F) - Manipulatives and pull out a deck of cards. Ask the class to come up with a couple of conditionals to use with the deck of cards like they did in "Conditionals with Cards." When the conditionals have been decided on as a class, fill in the blank part of the if
block with the various card values that the kids came up with. Examples include "King of Hearts", "Even Numbered", or "Diamonds". Fill in the action blocks with the actions the students came up with. Make sure the students know the action blocks need to be directly under the if
or else
block. Below is an example.
Now shuffle the deck of cards and play "Conditionals with Cards" again. Flip through the deck card-by-card, reacting to cards if a conditional has been made for it.
Preview of Online Puzzles
Pull up a puzzle from Course D, we recommend Lesson 11, Puzzle 9.
- Ask the class what the bee should do when it gets to the cloud.
- The bee should use a conditional to check for a flower or a honeycomb.
- Use the
if at flower / else
block. Ask the class what the bee should do if there's a flower. If there's not a flower, there will be a honeycomb. What should the bee do then?- The bee should
get nectar
if there is a flower andmake honey
if there is a honeycomb.
- The bee should
Fill in the rest of the code and press Run
. Discuss with the class why this worked.
Main Activity (30 min)
CSF Express Course - Website
While loops are not always a difficult concept for students to understand, but if you think your class might struggle with these puzzles, we recommend Pair Programming - Student Video. This will allow students to bounce ideas of each other before implementing the code. Pair programming works to increase confidence and understanding with topics like while loops.
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 do you feel about today's lesson?
- What is the difference between a while loop and a normal repeat loop?
- Give an example of a puzzle where you would use a while loop, but not use a repeat loop. Can you give an example of a puzzle where you would use a repeat loop, but not a while loop?
Extended Learning
While Simon Says
Go out to a large playing field and have the students stand in a line facing you. Make sure every student can see you. Declare a couple of "while loops" such as:
- While my right hand is up, you can walk toward me
- While I cover my eyes, you can skip toward me
- While my head is turned to the right, you have to walk backwards
The first student to get to you wins. If there's time, let other students be "Simon" at the front of the class.
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.