Lesson 11: Booleans and Conditionals
Overview
Students start by using booleans to compare the current value of a sprite property with a target value, using that comparison to determine when a sprite has reached a point on the screen, grown to a given size, or otherwise reached a value using the counter pattern. After using booleans directly to investigate the values or sprite properties, students add conditional if statements to write code that responds to those boolean comparisons.
Purpose
This lesson follows closely the booleans model that students first experienced in the Booleans Unplugged lesson. As before, we start with using booleans directly before using booleans to trigger if statements. In the following lesson we will introduce some boolean producing blocks, such as keyDown()
, which can be used in place of simple boolean comparisons to write programs that respond to user input.
Agenda
View on Code Studio
Objectives
Students will be able to:
- Predict the output of simple boolean statements
- Use conditionals to react to changes in variables and sprite properties
Vocabulary
- Boolean Expression - in programming, an expression that evaluates to True or False.
- If-Statement - The common programming structure that implements "conditional statements".
Introduced Code
Teaching Guide
Warm Up (5 min)
Answering Boolean Questions
Goal: At the end of the Boolean Question game from the previous lesson, students began adding conditions to their boolean questions - meaning that if the answer to the question is true, something should happen. Before programming with conditionals, we want to make sure that students have a solid understanding of what booleans really are.
Prompt:
- How many different numbers are there in the world?
- How many different words or combination of letters and other characters are there?
- How many different boolean values are there?
Discuss: Students should realize that the first two questions (numbers and strings), are essentially infinite, but that booleans are limited to two states.
Remarks
As you begin programming today, you'll be using booleans to make programs that change their behavior depending on the answer to those boolean questions.
Activity (40 min)
Content Corner
Though seemingly simple, understanding how a boolean statement will evaluate can be difficult given that different programming languages have differing opinions on 'truthiness' and 'falsiness'. In fact, JavaScript (the language used in this course) has two different operators to test boolean equality ==
and ===
.
The double equals operator (==
) is pretty generous in determining truthiness, for example each of the following is considered true
in JavaScript when using the ==
operator, but would be false
using the ===
operator:
1 == true; "1" == true; 5 == "5"; null == undefined; "" == false;
We use the ==
operator in this course because it's more forgiving, but it's important to be aware that it can sometimes report back truth when you really didn't intend it to (in which case you might want to use the more strict ===
operator)
Booleans Plugged
Transition: Send students to Code Studio.
Code Studio levels
Wrap Up (5 min)
Adding Conditionals
Journal: Think back to all of the programs you've written so far; how might you use conditionals to improve one of your programs from past lessons? What condition would you check, and how would you respond to it?
Standards Alignment
View full course alignment
CSTA K-12 Computer Science Standards (2017)
AP - Algorithms & Programming
- 2-AP-12 - Design and iteratively develop programs that combine control structures, including nested loops and compound conditionals.
- 2-AP-13 - Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
- 2-AP-17 - Systematically test and refine programs using a range of test cases.
- 2-AP-19 - Document programs in order to make them easier to follow, test, and debug.