Lesson 12: While Loops in Farmer


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.


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.


Warm Up (10 min)

Bridging Activity (15 min)

Main Activity (30 min)

Wrap Up (15 min)

View on Code Studio


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.


  • Play through the puzzles 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.

For the Teachers

For the Students


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


Report a Bug

Teaching Guide

Warm Up (10 min)


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 (15 min)

This activity will help bring the unplugged concepts from "Conditionals With Cards" into the online world that the students are moving into. It will also help bridge the concept of conditionals to a new type of loop- while loops. Choose one of the following to do with your class:

Unplugged Activity Using Paper Blocks

Print and cut out 2-3 while / do blocks 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." This time, instead of using the conditional in an if / else statement, they will be using it in a while loop.

When the conditionals have been decided on as a class, fill in the blank part of the while block with the various card groups that the kids came up with. Examples include "Even Numbered", "Red card", 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 inside the while block. Line the blocks up sequentially so students can see how it runs as a program.

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. To maintain authenticity for this while loop experience, make sure you hold up the selected card for a good long while to give students time to react over several beats. Feel free to jump back to previous cards to get the point across that once you have left a while loop, you continue moving forward and do not return to the previous loop just because the conditions match again.

Preview of Online Puzzles

Pull up a puzzle from today's online Code Studio puzzles. We recommend Puzzle 6.

  • Ask the class what the farmer should do when she gets to the pile of dirt.
    • She should use a while loop to start removing the dirt.
  • Use the while there is a pile / do block. Ask the class what the farmer should do within the while loop.
    • The farmer should remove 1. The farmer will keep "removing 1 dirt" while there is dirt. In other words, when there is no dirt, remove 1 will no longer execute!

Fill in the rest of the code and press Run. Discuss with the class why this worked.

Main Activity (30 min)

Online Puzzles

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


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?
  • Practice
  • 1
  • 2
  • 3
  • (click tabs to see student view)
View on Code Studio

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.

View on Code Studio

Student Instructions

View on Code Studio

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.

  • While Loops with the Farmer
  • 4
  • (click tabs to see student view)
  • Prediction
  • 5
  • (click tabs to see student view)
View on Code Studio

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.

View on Code Studio

Student Instructions

"I don't know how much dirt is in this pile!"

Help the farmer remove this entire pile using a while loop.

View on Code Studio

Student Instructions

View on Code Studio

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!

View on Code Studio

Student Instructions

  • Challenge
  • 10
  • (click tabs to see student view)
View on Code Studio

Student Instructions

Challenge: Fill all of these holes using as few blocks as possible.

  • Practice
  • 11
  • 12
  • (click tabs to see student view)
View on Code Studio

Student Instructions

View on Code Studio

Student Instructions

  • Prediction
  • 13
  • (click tabs to see student view)
View on Code Studio

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.

  • Levels
  • Extra
  • (click tabs to see student view)
View on Code Studio

Student Instructions

Fill all of the holes and remove all of the piles.

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.