Lesson 12: While Loops with the 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)

Bridging Activity (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

Links

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

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.

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

Bridging Activity (15 min)

Preview of Online Puzzles

Pull up a puzzle from the lesson, we recommend Puzzle 6.

  • Ask the class what the farmer should do when she gets to the pile of dirt.
    • The farmer should use a conditional in a while loop to check if there is a pile.
  • Use the while there is a pile / do block. Ask the class what the farmer should do if there is no pile. When should the farmer stop?
    • The farmer should remove 1 if there is a pile. The while loop will continue until there is no pile, so then your code is finished!

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

View on Code Studio

Student Instructions

The cloud is blocking the view. There could be a flower under this cloud. If there is a flower, the bee will need to collect nectar once. If there is not a flower the bee should do nothing. Take a good look at the code below. What do you think will happen after you click "Run"?

The bee will move to the cloud and try to get nectar, no matter what.

The bee will move to the cloud and only try to get nectar if there is a flower underneath.

The bee will move to the cloud and then do nothing, no matter what.

I don't know.

View on Code Studio

Student Instructions

More clouds!

Check underneath every cloud to see if it is hiding a flower before you get nectar. If there is a flower underneath the cloud, the bee will need to get nectar once.

Remember: Not all clouds hide the same thing!

View on Code Studio

Student Instructions

You can only collect nectar from flowers, but you can check any space to see if there is a flower. If there is a flower under any of these clouds, the bee will need to collect nectar once.

View on Code Studio

Student Instructions

In this puzzle, we know that every flower has exactly one nectar, but the flowers aren't spaced evenly.

Get all of the nectar using as few blocks as possible.

View on Code Studio

Student Instructions

View on Code Studio

Student Instructions

"Now I just want to make honey."

Some of these clouds might have honeycombs under them. Be sure to check if a honeycomb is hiding behind each cloud! If there is a honeycomb, the bee will only need to make honey once.

View on Code Studio

Student Instructions

Sometimes a cloud covers a flower, sometimes it covers a honeycomb!

Use the ifelse block to collect nectar at flowers and make honey at honeycomb. Remember: if there is a flower, the bee only needs to get nectar once. If there is a honeycomb, the bee only needs to make honey once.

View on Code Studio

Student Instructions

Look carefully at the code below. What do you think will happen after you click "Run"? Remember: there will only ever bee one honeycomb or one flower behind each cloud.

The bee will get nectar at each flower and honey at each honeycomb.

The bee will try to get nectar from both flowers and honeycomb.

The bee will try to get honey from both flowers and honeycomb.

I don't know.

View on Code Studio

Student Instructions

Challenge: There will be either a flower or a honeycomb under each of those clouds!

Collect nectar once if there is a flower. Otherwise, make honey once (because there is a honeycomb).

View on Code Studio

Student Instructions

Collect all of the nectar or make all the honey. You can only collect nectar from flowers and make honey from honeycombs. Check any space to see if there is a flower or honeycomb. There will only ever be one flower or one honeycomb behind each cloud.

View on Code Studio

Student Instructions

Conditionals can be helpful, even when you know exactly what is in each spot!

Collect all of the nectar and make all of the honey.

View on Code Studio

Student Instructions

Collect all of the nectar and make all of the honey. You can only collect nectar from flowers and make honey from honeycombs. Check every space to see if there is a flower or honeycomb.

View on Code Studio

Student Instructions

Collect all of the nectar and make all the honey. You can collect all of the nectar in one flower by using the while nectar > 0 loop.

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.