< Course D (2022)

Lesson 15: While Loops in Farmer

55 minutes

Overview

In this skill-building lesson, students will be working to fill holes and dig dirt in Farmer, but they will not know the size of the holes or the height of the mounds of dirt. To solve these puzzles, students will use a new kind of loop.

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.

CSTA K-12 Computer Science Standards (2017)
    • 1B-AP-11 - Decompose (break down) problems into smaller, manageable subproblems to facilitate the program development process.

Agenda

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.

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.

Teaching Guide

Warm Up (10 minutes)

Introduction

 

Display: Show “Reflect” slide

Reflect: When would you use a conditional statement in your daily life? For example: If it is cold outside, I will wear a jacket.

 

Display: Show “Discuss” slide

Say: While it is raining, I will use my umbrella.

Discuss:

  • What does the word “while” mean?

If I say “while there is a hole, fill it with dirt”

  • What would you do?
  • How long will you 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.

Display: Show the next slide

Say: Here are some times using a while loop would be useful:

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

Can you think of some others?

Preview of Online Puzzles

Display: Show “Preview of Online Puzzles” slide

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.

Vocabulary

  Display: Show “Vocabulary” slide

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

Main Activity (30 minutes)

While Loops in Farmer

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 off each other before implementing the code. Pair programming works to increase confidence and understanding with topics like while loops.

Display: Show “Level 1-3 - Skill Building” slide

Display: Show “Level 4 - While Loops with the Farmer ” video

Display: Show “Level 5 - Predict” slide

Display: Show “Level 10 - Challenge” slide

Display: Show “Level 11-12 - Practice” slide

Display: Show “Level 13 - Predict” slide

Display: Show “Level 14 - Challenge” slide

Lesson Extras

Display: Show “Lesson Extras slide

Wrap Up (15 minutes)

Reflection

Display: Show “Reflect” slide

Reflect: 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?

Creative Commons License (CC BY-NC-SA 4.0).

This work is available under a Creative Commons License (CC BY-NC-SA 4.0).

If you are interested in licensing Code.org materials for commercial purposes contact us.