Lesson 5
Conditionals Explore
To do: November 3
Reminder: On Friday -> Test (U3 and U4) on intro to programming - variables, strings/output, key words
To do: November 3 - Warm-up #1
Consider the following code snippet below written in pseudocode. Note that <- is the assignment operator (left arrow).
x <- 1
y <- 3
z <- 4
y <- y + 2
z <- z + y
x <- x + y + z
What are the values assigned to variables x, y, and z?
Warm-up #2
What is the output after this JavaScript code snippet is run? Note that the equal sign = is the assignment operator.
var num = 5;
num = num + num;
num = num * num;
num = num + num;
console.log( num );
Warm-up #3
What is the output after this JavaScript code snippet is run? Note that the equal sign = is the assignment operator.
var num = 10;
num = num + num;
num = num + num;
num = num + num;
console.log( num );
U4L04 Bubble 2: Photo Liker App Activity Guide
Unit 4 Lesson 5 - Warm Up
Prompt: On the whiteboard
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?
Unit 4 Lesson 5 - Activity
“hi”
“hi there”
“c u l8r”
Strings
Made of any characters
Inside double quotes
22
9
548
Numbers
Made of the digits 0...9
No quotes
Information can be stored as...
“123”
123
true
Boolean
true or false
Information can be also be stored as a...
false
Unit 4 Lesson 5 - Activity
evaluates to
<
This is a comparison operator. It’s a clue that we need to STOP and evaluate for a Boolean value.
Comparison operators:
< less than > greater than == equal to | <= less than or equal to >= greater than or equal to != not equal to |
3 is less than 8.
true
3
8
Unit 4 Lesson 5 - Activity
< 8
Each side of the comparison operator must be reduced to one value before we can compare
3 + 6 < 8
9
false
Unit 4 Lesson 5 - Activity
Do This:
Evaluate the expressions on each side of the comparison operator.
Then evaluate the expression for a Boolean value.
3
6
-
<
4
+
1
6
12
/
>
3
-
0
3
7
-
<=
10
5
9
+
==
4
+
10
3
*
)
(
Unit 4 Lesson 5 - Activity
Do This:
Evaluate the expressions on each side of the comparison operator.
Then evaluate the expression for a Boolean value.
3
6
-
<
4
+
1
6
12
/
>
3
-
0
3
7
-
<=
10
5
9
+
==
4
+
10
3
*
)
3
5
true
(
2
3
12
14
14
false
false
true
Unit 4 Lesson 5 - Activity
lives
5
Any of the values in an expression can be a variable.
Do This:
Evaluate the expression for a Boolean value.
score
10
lives +
<
4
score -
1
Note: Any expression that can be evaluate for true or false is known as a Boolean Expression.
Unit 4 Lesson 5 - Activity
lives
5
Any of the values in an expression can be a variable.
Do This:
Evaluate the expression for a Boolean value.
score
10
lives +
<
4
score -
1
5
10
9
9
false
Note: Any expression that can be evaluate for true or false is known as a Boolean Expression.
Unit 4 Lesson 5 - Activity
Decision time!
Can I go to the movies? I’m allowed to if it’s before 8 o’clock.
Do This:
Unit 4 Lesson 5 - Activity
Can I go to the movies? I’m allowed to if it’s before 8 o’clock.
false
true
time < 8
I can go to the movies! 😊
I can’t go to the movies. 😞
Unit 4 Lesson 5 - Activity
time < 8
Can I go to the movies? I’m allowed to if it’s before 8 o’clock.
false
true
I can go to the movies! 😊
I can’t go to the movies. 😞
It’s 9 o’clock. Can I go to the movies?
9
Comparison operator alert!
Stop and evaluate the Boolean expression.
Assign 9 to the variable time
Unit 4 Lesson 5 - Activity
time < 8
Can I go to the movies? I’m allowed to if it’s before 8 o’clock.
false
true
I can go to the movies! 😊
I can’t go to the movies. 😞
It’s 9 o’clock. Can I go to the movies?
9
9
false
Comparison operator alert!
Stop and evaluate the Boolean expression.
Assign 9 to the variable time
Unit 4 Lesson 5 - Activity
Unit 4 Lesson 5 - Warm Up
onEvent("downButton", "click", function( ) {
likes = likes - 1;
setText("likeCounter","Likes: " + likes);
});
Unit 4 Lesson 5 - Warm Up
onEvent("downButton", "click", function( ) {
if(numLikes>0)
{
numLikes = numLikes - 1;
}
setText("likeCounter","Likes: " + likes);
});
What if my decision requires several steps?
Can I adopt a cat?
I can if I have 40 dollars or more
AND
I am over 14 years old
Unit 4 Lesson 5 - Activity
Can I adopt a cat?
I can if have 40 dollars or more
AND
I am over 14 years old
Do This:
Unit 4 Lesson 5 - Activity
false
true
age > 14
I can adopt a cat! 😊
I can’t adopt a cat. 😞
Let’s try it out. I am 17 years old and I have $39 dollars.
Can I adopt a cat? I can if I have 40 dollars or more AND I am over 14 years old.
age
money
money >= 40
true
17
39
Unit 4 Lesson 5 - Activity
false
false
true
age > 14
I can adopt a cat! 😊
I can’t adopt a cat. 😞
Let’s try it out. I am 17 years old and I have $39 dollars.
Can I adopt a cat? I can if I have 40 dollars AND I am over 14 years old.
age
money
money >= 40
true
false
17
39
17
true
39
false
Unit 4 Lesson 5 - Activity
Logical Operators:
&& AND
|| OR
! NOT
Boolean values are a type of information, so they can also be evaluated in a Boolean expression using logical operators.
true
false
&&
If something is true and (&&) false, it evaluates to false.
Unit 4 Lesson 5 - Activity
Logical Operators:
&& AND
|| OR
! NOT
Boolean values are a type of information, so they can also be evaluated in a Boolean expression using logical operators.
true
false
&&
false
If something is true and (&&) false, it evaluates to false.
Unit 4 Lesson 5 - Activity
Let’s take a look at Truth Tables
In the next few slides, Boolean values will be blue, but true will be a dark blue, and false a light blue.
true
false
Unit 4 Lesson 5 - Activity
Truth Tables - used in evaluating Boolean expressions
&& AND
true
false
&&
false
true
true
&&
true
false
true
&&
false
false
false
&&
false
true and true evaluate to:
true and false evaluate to:
false and true evaluate to:
false and false evaluate to:
Unit 4 Lesson 5 - Activity
true
false
false
false
Truth Tables - used in evaluating Boolean expressions
&& AND
true
false
&&
false
true
true
&&
true
false
true
&&
false
false
false
&&
false
true and true evaluate to:
true and false evaluate to:
false and true evaluate to:
false and false evaluate to:
Unit 4 Lesson 5 - Activity
true
false
false
false
&&
Both must be true for the Boolean expression to evaluate as true.
|| OR
true
false
||
true
true
||
false
true
||
false
false
||
true or true evaluates to:
true or false evaluates to:
false or true evaluates to:
false or false evaluates to:
true
true
true
false
true
true
true
false
||
Either may be true for the Boolean expression to evaluate as true.
Unit 4 Lesson 5 - Activity
The ! NOT table is easy! The results are the opposite of the Boolean value.
! NOT
true
!
false
!
not true evaluates to:
not false evaluates to:
false
true
false
true
Unit 4 Lesson 5 - Activity
p | q | p && q�(p AND q) | p || q�(p OR q) | !p�(NOT p) |
true | true | true | true | false |
true | false | false | true | false |
false | true | false | true | true |
false | false | false | false | true |
The Truth Table: p and q are Boolean variables. Write in IN
Use logical operators to combine several Boolean expressions into one expression.
Can I adopt a cat?
I can if I have 40 dollars or more
AND
I am over 14 years old
Unit 4 Lesson 5 - Activity
false
money >= 40 && age > 14
I can adopt a cat! 😊
I can’t adopt a cat. 😞
Let’s try it out. I am 15 years old and I have $40 dollars.
Can I adopt a cat? I can if I have 40 dollars or more AND I am over 14 years old.
age
money
true
15
40
true
true
&&
Truth Table
Unit 4 Lesson 5 - Activity
false
money >= 40 && age > 14
I can adopt a cat! 😊
I can’t adopt a cat. 😞
Let’s try it out. I am 15 years old and I have $40 dollars.
Can I adopt a cat? I can if I have 40 dollars AND I am over 14 years old.
age
money
true
15
40
40
15
true
true
&&
&&
true
true
true
Truth Table
Unit 4 Lesson 5 - Activity
true
Key Takeaways
10
2
evaluates to
<
false
4
4
evaluates to
==
true
Unit 4 Lesson 5 - Activity
Key Takeaways
true
false
evaluates to
&&
false
true
false
evaluates to
||
true
Unit 4 Lesson 5 - Activity
Homework:
Reminder: Test (U3 and U4) on intro to programming - variables, strings/output, key words