Lesson 9: 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)
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 Course D Online Puzzles - 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
- Course D Online Puzzles - Website
- CS Fundamentals Main Activity Tips - Lesson Recommendations
For the Students
- 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.
Main Activity (30 min)
Course D Online Puzzles - 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 normalrepeat
loop? - Give an example of a puzzle where you would use a
while
loop, but not use arepeat
loop. Can you give an example of a puzzle where you would use arepeat
loop, but not awhile
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.
Student Instructions
"Hi, I'm a farmer. I need your help to flatten the field on my farm so it's ready for planting!"
Move to the pile of dirt and use the remove
block to remove it.
Student Instructions
Student Instructions
"Move to the pile of dirt and tell me how many shovelfuls to remove."
Use as few blocks as possible to solve this puzzle.
Student Instructions
Look closely at the code below. What will happen after you click "Run"?
Nothing will happen.
The farmer will never stop removing dirt.
The farmer will remove dirt as long as there is a pile, then stop when the pile is gone.
I don't know.
Student Instructions
"I don't know how much dirt is in this pile!"
Help the farmer remove this entire pile using a while
loop.
Student Instructions
Student Instructions
Look at all of those holes! Each one needs a different amount of dirt.
You can use the while
loop to easily fill them all!
Student Instructions
Student Instructions
Challenge: Fill all of these holes using as few blocks as possible.
Student Instructions
Student Instructions
Student Instructions
Take a good look at the code below. What will be left at the end of this puzzle?
Nothing. This code will leave only level ground.
This code will leave the pile completely untouched.
This code will leave the holes completely untouched.
I don't know.
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
- 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.3 - Ask and answer questions about information from a speaker, offering appropriate elaboration and detail.
- 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.4 - Model with mathematics
- 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.3 - Use multiplication and division within 100 to solve word problems in situations involving equal groups, arrays, and measurement quantities, e.g., by using drawings and equations with a symbol for the unknown number to represent the problem.1
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.