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
- Prediction
- 2
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.
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!
Student Instructions
Let's try that again, but with more piles!
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!
Student Instructions
Pick all of the lettuce.
- Repeat Unit Statements
- 7
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.
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.
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
- Challenge
- 12
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
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.
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
- 4.L.6 - Acquire and use accurately grade-appropriate general academic and domain-specific words and phrases, including those that signal precise actions, emotions, or states of being (e.g., quizzed, whined, stammered) and that are basic to a particular topic (e.g
SL - Speaking & Listening
- 4.SL.1 - Engage effectively in a range of collaborative discussions (one-on-one, in groups, and teacher-led) with diverse partners on grade 4 topics and texts, building on others’ ideas and expressing their own clearly.
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.6 - Attend to precision
- MP.7 - Look for and make use of structure
- MP.8 - Look for and express regularity in repeated reasoning
NBT - Number And Operations In Base Ten
- 4.NBT.4 - Fluently add and subtract multi-digit whole numbers using the standard algorithm.
Next Generation Science Standards
ETS - Engineering in the Sciences
ETS1 - Engineering Design
- 3-5-ETS1-1 - Define a simple design problem reflecting a need or a want that includes specified criteria for success and constraints on materials, time, or cost.
- 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.