Booleans and Conditionals

It takes a quarter, but by this time my semester-long CS class is really moving along well.

After the kids learned about variables and took their quiz, I decided tackle boolean expressions and “if” statements with them. During testing week, we had very short classes, so the introduction activities were very short and focused. This was OK for the time we had.

Lesson 1: Boolean Expression exploration. Students had used variables to store text and numbers, but the idea of the value true/false as information was new. I gave them this program and asked them to un-comment the commented lines, one at a time, and try to explain what those lines were doing.

Boolean Expression Starter

We had a class discussion, especially focusing on these lines. The output is “a>b is false”. Why does it substitute “false” for the variable bool? The variable is assigned to the result of “a>b” which is false.

``` var a = 3; var b = 4; var bool = (a > b); ```
``` fill(112, 4, 112); textSize(30); text("a>b is " + bool, 50,100); ```

In their math classes, we usually don’t explore equations and inequalities in a situation where they may be false, so this understanding is new – that the > operator is a logic operator that has a result, and that result can be one of two values. For that matter, a math equation such as “3x + 2 = 8” is a true/false expression. In a computer program, you’d write it as “3 * x + 2 === 8” and it will evaluate to true for only one value of x. For any other value of x, you’ll get a value of false. Solving the classic two-step equation is identifying which sole value of x will make the expression evaluate to true.
``` ```In the rest of the program, we explore the difference between numeric data and text, and I have the students look at other operators used in boolean expressions: < and >, <= and >= (greater than or equal to / less than or equal to), ===, !== (not equal to), && (logical AND) and || (logical OR).
``` ```

Lesson 2: Boolean worksheet and review of variables

I put the students into pairs and assigned them to this worksheet, done as a collaborative document.

var and boolean worksheet

The first page is on understanding variables – how assignment works and syntax. It gives them a chance to brush up on some middle school math standards as well!

The second page is on boolean expressions, and I included examples with properties of arithmetic – again, to review topics from math class and to encourage the kids to discuss and work out misconceptions about booleans.

We went over the answers and discussed the worksheet. These lessons were great for our brief instructional time on testing days.  Kids found these activities engaging – tough but not too tough.

Lesson 3: Intro to “if” statements

I gave students this program and tasked them with changing the program so it showed a happy face instead of a sad face.

If Statement Starter: Happy Face

Many students figured this out really quickly, and they did this in various ways – either by changing the value of the variable “a”, by changing the boolean expression from “a>10” to “a<10” or something else true, or (in a couple of cases) changing the arc’s parameters so it made a smile instead of a frown!

At this point I did a bit of direct teaching about the “if” statement and its companion, “if/else”. If the boolean expression in the parentheses evaluates to true, the code inside the curly braces {} will be run. Otherwise, the code is skipped.  In an if/else, there’s an alternate set of code in another set of curly braces, if the boolean expression is false.

I paired the students up and gave them the rest of the tasks in the program – change the program so it shows a happy face and uses the === operator, then uses the !== operator, then uses &&, and then uses ||.

An interesting thing came up during the discussion here! Some students wrote boolean expressions such as this one, which are nonsense boolean expressions but displayed the happy face anyway.
```var a = 5; var b = 10; if(a && b > 0) { arc(200,210, 50,50,0,180);```

}

The program would first evaluate b>0, which is TRUE, and then it would evaluate “5 AND true”, which is a nonsense boolean expression to us, but in JavaScript, 5 must evaluate to true because the happy face does display. But the expression doesn’t mean what the student thought it meant – it’s not the same as “a>0 && b>0”. But since the output was correct, the students were totally satisfied with their solution. This is a misconception I’ll really have to watch for. The conversations you have when a student’s mental model is wrong, but their output is still correct, are really fascinating and time-consuming and can either be really great or a big power struggle, or sometimes both.

I finished this set of lessons with a Kahoot! quiz, and the kids did reasonably well on it, so I was excited to move on to their Prisoner’s Dilemma programs for the following lesson – I’ll write about that in the next blog post.

Kahoot! on if statements, variables, and booleans
Lesson 4: The Coin Flipper

This activity was very teacher-directed and basically involved me modeling how to write a program that produced a random dice roll, and then a random coin flip – while the students followed along. It didn’t feel like a great lesson at the time, but it had some positives.

– Kids were on task and engaged pretty much the whole time.
– There’s something to just typing in a canned program. I don’t know what it is, but in my own experience, I got a lot out of doing this when I was a kid. My mom and I would find a program in a magazine, and I would just type it in right from the magazine pages. I’d find myself thinking about the code as I typed it in and understanding it far better than if the program had just appeared on my computer. It’s not the same level of cognitive demand as creating a program from scratch – but when you don’t have the tools to create a program from scratch, for some reason, typing in a canned program helps you develop the skills to use those tools.

So we did, and the kids saved their coin flipper programs to their libraries. I wanted them to understand the random() function and feel more confident with if statements, and this lesson actually seemed to work pretty well.

Coin Flipper

In the past, I hadn’t spent a lot of time working with the students on boolean expressions and operators – I was glad we did that this time around, so even though these lessons took the better part of a week, the kids were in a better position to really understand and use conditionals.