Lesson 4: Conditionals with the Farmer

Overview

This lesson introduces students to while loops and if / else statements. While loops are loops that continue to repeat commands as long as a condition is true. While loops are used when the programmer doesn't know the exact number of times the commands need to be repeated, but the programmer does know what condition needs to be true in order for the loop to continue looping. If / Else statements offer flexibility in programming by running entire sections of code only if something is true, otherwise it runs something else.

Purpose

A basic understanding of conditionals is a recommended prerequisite for Course E. We created this introduction to give a review for the students already familiar to conditionals and allow practice for the students that are just learning. If you find that the understanding of conditionals varies widely in your classroom, we recommend a strategic pairing of students when completing this online lesson.

Agenda

Warm Up (15 min)

Main Activity (30 min)

Wrap Up (15 min)

Extended Learning

View on Code Studio

Objectives

Students will be able to:

  • Define circumstances when certain parts of a program should run and when they shouldn't.
  • Determine whether a conditional is met based on criteria.

Preparation

  • Play through the puzzles to find any potential problem areas for your class.
  • Make sure ever student has a journal.

Vocabulary

  • Condition - Something a program checks to see if it is true before allowing an action.
  • Conditionals - Statements that only run under certain conditions.
  • While Loop - A loop that continues to repeat while a condition is true.

Support

Report a Bug

Teaching Guide

Warm Up (15 min)

Introduction

Gather the class together and ask two volunteers to walk straight in some direction in the classroom. If they encounter a chair out of place, they must step over it. If they reach a wall, they must sit down.

Once all of the students are sitting down, ask how you would program a robot to respond to a wall or a chair. Remind students that you cannot simply say "Step over chair" unless you know there is a chair, and you will not always know there is a chair. It might be helpful to translate the task into instructions like:

  • while there is a path ahead
    • walk forward
    • if there is a chair, step over it
  • sit down

Tell students they will be using conditionals to solve this problem on Code.org. Give the definition of:

  • Condition: A statement that a program checks to see if it is true or false. If true, an action is taken. Otherwise, the action is ignored.
  • Conditionals: Statements that only run under certain conditions.

Open up a discussion of when you might use a conditional in your code.

Main Activity (30 min)

Online Puzzles

The patterns in these puzzles may not be obvious to every student. We recommend that you play through these levels beforehand to best understand any problem areas for your class. Also, watching and using the techniques from Pair Programming - Student Video may be helpful for your class.

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 a conditional? Why would you program a conditional?
  • Give an example of using a conditional during your day? (ex. If I am hungry, I eat some food; While I am walking across the street, I keep an eye out for cars)

Extended Learning

While We Play

Gather the class for some fun outside or in a gym with a ball! This could be done in a circle or as a team in a court

Rules:

  • While the ball is in play, we must all be ready to hit it
  • If the ball is hit to you, you must keep it in the air
  • If you hit the ball once, you cannot hit it again (only one touch per person per turn, no double hitting)
  • If the ball goes out of bound, every student must fall to the ground dramatically. The last kid to fall has to retrieve the ball.

At the end of the first round, ask the students if they can identify the conditionals in the game. Can they come up with others they might want in the game?

  • While Loops with the Farmer
  • 1
  • (click tabs to see student view)
  • Prediction
  • 2
  • (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

Oh my! This pile is so big that it's hard to guess how much dirt is in it.


We've added a new block to the toolbox called the while there is a pile block. Use this to remove dirt only while there is still some to scoop!

View on Code Studio

Student Instructions

Let's try that again, but with more piles!

View on Code Studio

Student Instructions

Now the harvester needs to pick all of the lettuce. Use while there is lettuce inside of a repeat loop to get it all!

View on Code Studio

Student Instructions

Pick all of the lettuce.

  • Repeat Unit Statements
  • 7
  • (click tabs to see student view)
  • Practice
  • 8
  • 9
  • 10
  • (click tabs to see student view)
View on Code Studio

Student Instructions

The lettuce is now only at the end of the path.
Travel down the path until you reach a head of lettuce, then continue to pick it while there is still some left.

View on Code Studio

Student Instructions

The lettuce is scattered all over the garden.
Travel down each path until you reach a head of lettuce, then continue to pick it while there is still some left.

View on Code Studio

Student Instructions

The lettuce is scattered all over the garden.
Travel down each path until you reach a head of lettuce, then continue to pick it while there is still some left.

  • "If/Else" with the Bee
  • 11
  • (click tabs to see student view)
  • Challenge
  • 12
  • (click tabs to see student view)
View on Code Studio

Student Instructions

Challenge: The only way to get all of the crops is to move forward until you get to the lettuce, then turn left if there is a path to the left. Otherwise, turn right.

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

Student Instructions

Move forward until you get to the lettuce, then turn left if there is a path to the left. Otherwise, turn right.

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.