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

Main Activity (30 min)

Wrap Up (15 min)

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 the puzzles to find any potential problem areas for your class.
  • Make sure every student has a journal.

Links

Heads Up! Please make a copy of any documents you plan to share with students.

For the Teachers

For the Students

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

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