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

## Objectives

### Students will be able to:

• 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

• `If statement`
• `Equality operator`
• `Inequality operator`
• `Greater than operator`
• `Greater than or equal operator`
• `Less than operator`
• `Less than or equal operator`

## Warm Up (5 min)

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.

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)

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

• Lesson Overview
• Make a Prediction
• Video: Booleans

## Discussion Goals

Students should be able to explain that a Boolean expression is something that is either true or false, similar to a yes or no question. The more formal way to say this is that Boolean expressions evaluate to either true or false. That means that when the computer processes a Boolean expression, it checks to see whether the expression describes a situation that is true or false, and then uses the value of either true or false wherever the expression is found.

Some examples of Boolean expressions that evaluate to true are `3 > 1` and `4 <= 7`, but press students to think of expressions that might be better represented by variables, such as `studentAge < 70` or `sizeOfClass > 2`.

Some examples of Boolean expressions that evaluate to false are `4 == 7``schoolName == "Hogwarts"`, and `currentYear < 1000`.

• Boolean Comparison
• Booleans and Comparison Operators
• Video: Conditional Statements

## Discussion Goals

The broad point of this question is that programmers use if statements when they want the program to run differently in response to different situations. Encourage students to think of particular situations in which this would be the case. For example, they might want their characters to move faster when a “bonus” is in effect, or maybe they want more enemies to appear when the player reaches a certain level. Maybe they want the program to react in some way if a user presses a key or clicks the mouse, or they want a character to change animations if it touches a particular item.

• Basic Conditionals
• If Statements
• Levels