Variables, Conditionals, and Functions - Lesson 5: Conditionals Explore

Overview

Students learn the basics of conditionals through an unplugged activity using the sticky notes and plastic baggies from the Variables Explore lesson. Flowcharts are introduced as a way to understand how computers make decisions using Boolean expressions.

Goals

Students will be able to:

  • Use appropriate vocabulary to describe Boolean expressions and conditional statements
  • Evaluate expressions that include Boolean values, comparison operators, and logical operators
  • Trace simple programs that use Boolean expressions and conditional statements

Purpose

The warm up activity is designed to provide context for the Conditionals progression. The subsequent activity provides students a physical mental model they will be able to use when they start programming with conditionals in the subsequent lessons.

Resources

Getting Started (5 minutes)

Discuss: Imagine you want to make a decision about what to wear to an event. Name two pieces of information you'd want. How would you use them in your decision?

Activity (30 minutes)

Group: Group students in pairs.

Distribute: Give each pair of students:

  • A small stack of red, yellow, and blue sticky notes
  • A pen/pencil
  • 3 plastic baggies
  • A dry erase marker to share with another group
Teaching Tip

Supplies Substitutions: There's no need to use stickie notes if you have other scraps of colored paper. Also consider cutting stickies in 4 to make them go further. If you don't have dry erase markers handy consider using pieces of masking tape on the baggies.

Guided Activity: Today's activity introduces students to the concept of variables. As a visual aid, you can use Code.org's presentation slides for Unit 4, Lesson 5: Conditionals Explore. These slides include animations. The notes below describe when to move to the next slide or click through an animation -- if you aren't using the slides, you can ignore these prompts.

Teaching Tip

Running the Activity: This activity asks students to follow along as a number of core concepts for programming are introduced. The model is typically that a term or concept is introduced and modeled and then afterwards students are encouraged to try it out on their own. Trying it out typically means they are writing information on a sticky note and sharing it with another group before discussing the results with the whole class.

If you're using the slides to accompany this activity, slides with animations have an icon in the bottom left corner to let you know you need to click to reveal more of the slide's content.

To help you more easily prepare the activity and keep track of your instructions, detailed instructions have been included as speaker notes in the presentation. Here are some tips to help you throughout the presentation.

  • There are opportunities throughout the presentation for students to actively engage. At these moments students should be making things with their manipulatives or using them to answer questions. Use these opportunities to check progress.
  • There is a fair amount of new vocabulary introduced but it is introduced gradually and with intentional repetition. Make a point of actively modeling the use of new terms.
  • The most important goal here is building a mental model. It is ok if students have some open questions that will get resolved over the subsequent conditional lessons.
  • Both you and students can use the "Key Takeaways" to check your understanding at the end.
  • Students who have been using Quorum in the previous lessons will have already seen basic conditionals as part of their event-handling code, but they haven't been given context as to how they work. You can reference back to these examples, or just leave them alone for now.
  • The slides use Javascript instead of Quorum. The concepts of conditionals and booleans are valid regardless of language, but when students program, they should know how to use the Quorum symbols. Here's the differences between the two languages:
    • - The "equal to" operator in Quorum is "=" instead of "=="
    • - The "not equal to" operator in Quorum is "not=" instead of "!="
    • - The "AND" operator in Quorum is "and" instead of "&&"
    • - The "OR" operator in Quorum is "or" instead of "||"
    • - The "NOT" operator in Quorum is "not" instead of "!"

Guided Activity

Follow this lesson with the supplemental slides.

Slide: Conditionals Explore

Say: Today we are going to explore Conditionals.

Slide: Information can be stored as...

Say: Previously, we learned that information can be stored as numbers or strings in variables. These are two different types of data. Numbers and strings can be combined together using operators to make expressions. Expressions are evaluated before storing in variables.

Slide: Information can be also be stored as a...

Say: Boolean values are another type of data. They can store the values "true" or "false". Why would we want that information? Booleans are used to make decisions. If something is true, do this. If something is false, do that.

Click for animation.

Say: You only need two blue sticky notes today. Write down "true" on one and "false" on the other.

Slide: 3 < 8

Say: Let's look at this expression: 3 < 8. Take a guess as to what Boolean value this evaluates to.

Do This: Hold up the sticky note with the correct value.

Click for animation.

Say: The expression evaluates to true, because 3 is less than 8.

Click for animation.

Say: The less than symbol is a comparison operator. When we see a comparison operator, we need to stop and evaluate for a Boolean value. There are six different comparison operators - you may be familiar with some of them from math class. These symbols are less than, greater than, less than or equal to, greater than or equal to, equal to, and not equal to. The symbols on the slide show how these appear in Javascript, but they look different in Quorum. You'll get a chance to practice with the Quorum symbols later.

Slide: Each side of the comparison operator...

Say: Both sides of the relational operator should be reduced to a single value before we can compare. Think of it in terms of sticky notes. You should have only one sticky note on each side.

Do This: What does 3 + 6 equate to?

Click for animation.

Say: Right: 9! Now we can compare 9 and 8 using the relational operator: less than.

Do This: Does this evaluate to true or false? Hold up the correct sticky note.

Click for animation.

Slide: Do This:

Say: Let's evaluate these expressions line by line. It may be helpful to use your sticky notes here.

  1. 6 - 3 < 4 + 1
  2. 12 / 6 > 3 - 0
  3. (7 - 3) * 3 <= 10
  4. 9 + 5 = 4 + 10

Do This: Reduce each side of the relational operator to one sticky note. Then evaluate for a Boolean value. Compare your answers with a partner.

Click for animation: When the class is finished, click through to view the answers. The third line may be a little tricky. Remind students to evaluate the information in the parenthesis first. In the fourth line, draw attention to the double equal signs used in the slide and remind the class double equal signs are a signal to compare if both sides are the same.

Slide: Any of the values in an expression can be a variable.

Say: The expressions that we evaluate can also contain variables. Remember, variables store information. This information can be different data types like numbers, strings, or even Boolean values themselves. For now, we are going to stick with numbers.

Do This: With a partner, look at these variables and the expression. Reduce both sides of the relational operator, then evaluate for a Boolean value.

  1. lives: 5
  2. score: 10
  3. lives + 4 < score - 1

Click for animation: When the class is finished, click through for the answer.

Say: Any expression that can be evaluated as true or false is known as a Boolean Expression.

Slide: Decision time!

Say: At the beginning of class, we talked about how Booleans are used to make decisions. Let's see that in action. Consider the following question: Can I go to the movies? I'm allowed to if it's before 8 o'clock. This may seem like a simple decision! But let's think about this like a computer.

Do This: What information would the computer need to know in order to answer the question. Create a baggie variable to store the information. Give it a name.

Slide: "Can I go to the movies" flowchart

Say: We can think of the decision like a flowchart. It's an organized way to make a decision or come to a conclusion. Creating a flowchart helps us think like a computer. At the top, we are listing the variables that are needed. Your variable might be called time or clock or really anything. It doesn't matter as long as you use the same name every time you refer to the variable. For this example, I'm naming my variable "time". Let's step through the flowchart with an example to see how it works.

Slide: "Can I go to the movies" flowchart #2

Click for animation.

Say: It's 9 o'clock. Can I go to the movies?

Click for animation.

Say: First I assign 9 to the variable time, then I move on to the diamond section.

Click for animation.

Say: I see a comparison operator, which let's me know that we need to stop and evaluate the Boolean expression.

Click for animation.

Say: First reduce both sides to a single value. Then evaluate for a Boolean value. In this case, The Boolean expression evaluates to false.

Click for animation.

Say: Follow the "false" path, and we arrive at the result: "I can't go to the movies."

Note: If students are confused, return back to the previous slide and try running some examples together as a class. Students can hold up true or false when evaluating the Boolean expression. Once students get the hang of using a flowchart, continue on to the next slide.

Slide: Have I won the game?

Say: Here's another decision that needs to be made. I'm not sure if I have won the game I'm playing. I have won it if my score * (times) my lives is greater than 10.

Do This: Direct students to create baggie variables for the information that needs to be stored and write down the Boolean expression on a spare sticky note or scrap paper. Groups then compare their Boolean expressions.

Slide: "Have I won the game?" flowchart

Say: Take a look at the Boolean expression in the decision section of the flowchart: score * lives > 10. Is this similar to what you and your partner wrote? Are there other ways we could construct this? (score and lives might have different names, score and lives might be in opposite order, you could write it as 10 < score * lives).

Click for animation.

Do This: With a partner, try following the flowchart with a few different inputs.

Slide: Challenge!!

Say: Here's a new decision we need to make:

Is my dog older than me if his age is converted to human years? Seven dog years equals one human year.

Do This: With a partner walk through these steps. Think about what the Boolean expression will look like.

  1. What information do I need to know?
  2. Create baggie variable(s) to store that information. Give them names.
  3. With a partner, discuss what the Boolean will look like. What will be compared?

Slide: "Is my dog older than me" flowchart

Say: Let's use a flowchart for the question we just considered.

Do This: With your partner, step through a flowchart with a few different inputs.

Note: After students have practice on their own, call on one group to give input values for dogAge and myAge and step through the flowchart as a class.

Slide: What if my decision requires several steps?

Say: What happens if my decision requires several steps? I want to adopt a cat, but I have to have both 40 dollars and be over 14 years old.

Note: Emphasize precise language here. The Boolean is checking if I have 40 dollars - that's exactly 40, not one cent more! In the Conditional Practice lesson, students will return to this flowchart and edit it to account for having more than 40 dollars.

Slide: Can I adopt a cat?

Say: Let's get setup for stepping through the decision making process for this question:

Can I adopt a cat? I can if I have 40 dollars AND I am over 14 years old.

Do This: Set up your baggies, and discuss with a partner how the flowchart might be set up.

  1. What information do I need to know?
  2. Create baggie variable(s) to store that information. Give them names.
  3. With a partner, discuss what the Boolean will look like. What will be compared?

Slide: "Can I adopt a cat?" flowchart

Say: This flowchart looks a little different from the previous one. There are two stopping points where a Boolean expression is evaluated. Let's try this out with an example.

Click for animation: Click through, one step at a time with the class. When you get to a decision point, before clicking to the next animation have students evaluate the expression and hold up a blue sticky note.

Say: Notice in the question box, I've stated that I can adopt the cat if I have 40 dollars AND (emphasize) I am over 14 years old. I am checking two things. Let's look at a way to simplify that process.

Slide: Boolean values are a type of information...

Say: In the previous example, we had two Boolean expressions to evaluate. We can use logical operators to compare the results of those Boolean expressions. The logical operators we will look at are AND, OR, and NOT.

Note: If you're using the slides, the symbols shown are for Javascript. In Quorum, these logical operators are and, or, and not.)

Slide: Let's take a look at Truth Tables

Say: Now we're going to take a look at something called a Truth Table.

Slide: Truth Table (AND)

This is a truth table.

  • true and true = true
  • true and false = false
  • false and true = false
  • false and false = false

Say: The table demonstrates all the possible combinations of true and false and what they would evaluate to if combined with the logical operator AND. The first one seems obvious to us - if something is true and another thing is true, then the whole thing is true!. Read through each row, and consider what is being stated. In rows 2 and 3, if one value is false, the whole thing evaluates to false. This is what happened with the cat example. On the final line, if both values are false, the final evaluation is false.

Click for animation.

Say: What's the takeway? In evaluating two Boolean expressions with the AND operator, both must be true in order for the whole expression to evaluate as true.

Slide: Truth Table (OR)

Say: Now let's consider a Truth Table for OR.

  1. true or true
  2. false or true
  3. true or false
  4. false or false

Do This: With a partner, evaluate each row. For example, if the first sticky is true or the second sticky is true what do you think the whole expression will evaluate to: true or false?

Click for animation: Stop line by line and allow time for groups to make their predictions.

Say: For the OR Truth Table, if either sticky note is true, the expression evaluates to true.

Slide: Truth Table (NOT)

Say: The NOT table is simple because it reverses the value.

Click for animation: As a class quickly state what the opposite values are and click through the animations.

Slide: Use logical operators to combine...

Say: Let's return to the cat example. How can we write a single expression that accounts for all the information that needs to be checked?

Can I adopt a cat? I can if I have 40 dollars AND I am over 14 years old.

Do This: With a partner, create the expression using logical operators.

Note: It's ok if students struggle with this activity. After a few minutes, move on to the next slide and reveal the answer. This will help students make the connection.

Slide: "Can I adopt a cat?" flowchart

Click for animation: Click through the animation step by step and follow along as a class. Allow time to predict at decision points. When the truth table (yellow box) appears on the screen, take a moment to remind students what this is and what it demonstrates. If a value is true and another value is true, it evaluates to true. It's not expected that students will memorize the truth tables, but they should be familiar with how they work.

Note: If you have extra time, try running this flowchart with other inputs. Try changing the conditions and then running the flowchart (i.e. I can adopt the cat if I have 40 dollars OR I am over 14 years old)

Say: Today we used flowcharts to demonstrate how computers make decisions. In the next class, we will investigate how decisions are made in code.

Slide: Do This: Now it's your turn to make a flowchart.

Do This: Give students a few minutes to craft their own flowcharts and test on each other following these instructions:

  • Think back to the warm-up where we discussed making a decision on what to wear to an event.
  • Write down the variables at the top.
  • Below the variables, create the Boolean expression that will be used to make the decision.
  • Draw True/False lines to the possible decisions.
  • Challenge: Use logical operators (and, or, not) in your Boolean expression. Add extra branches with multiple decisions.
  • Test your flowchart with a friend!

Note: Simple flowcharts are fine, or students can build more complex models. Students are ready to move on when the majority of the class has made and tested a flowchart.

Slide: Key Takeaways

Click for animation: Click through the animation to reveal the key takeaways on the slide.

Review: Review the key takeaways with students:

  • A Boolean Value is a data type that is either true or false.
  • Comparison Operators <, >, <=, >=, =, not= indicate a Boolean expression.
  • Each side of the Boolean expression is reduced to a single value.
  • Single values are compared and result in a Boolean value (true or false)

Slide: Key Takeaways (#2)

Click for animation: Click through the animation to reveal the key takeaways on the slide.

Review: Review the key takeaways with students:

  • Boolean expressions can also include Logical Operators (AND, OR, NOT). Both sides of the logical operator are reduced to a single Boolean value
  • A truth table is used to evaluate the reduced Boolean expression to a single Boolean value
  • A decision is made with the single Boolean value
  • A flowchart illustrates the steps of making a decision with a Boolean expression

Wrap Up (10 minutes)

Video: As a class, watch CS Principles: Conditionals - Part 1 Boolean Expressions (Video).

Journal

Have students add the following words and definitions to their journals:

  • Boolean Value: True or false
  • Boolean Expression: An expression that evaluates to either true or false

Assessment: Check for Understanding

For Students

Open a word doc or google doc and copy/paste the following question.

Question

Can a computer evaluate an expression to something between true and false? Can you write an expression to deal with a "maybe" answer?

Standards Alignment

  • CSTA K-12 Computer Science Standards (2017): 2-AP-10, 3A-AP-15
  • CSP2021: AAP-2.E.1, AAP-2.F.5

Next Tutorial

In the next tutorial, we will discuss CSP Unit 4 Lesson 6 Conditionals Investigate, which describes Investigate the use of branching conditionals in apps.