1 of 40

Lesson 5

Conditionals Explore

2 of 40

To do: November 3

Reminder: On Friday -> Test (U3 and U4) on intro to programming - variables, strings/output, key words

3 of 40

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?

4 of 40

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 );

5 of 40

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 );

6 of 40

U4L04 Bubble 2: Photo Liker App Activity Guide

7 of 40

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?

8 of 40

9 of 40

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

10 of 40

true

Boolean

true or false

Information can be also be stored as a...

false

Unit 4 Lesson 5 - Activity

11 of 40

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

12 of 40

< 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

13 of 40

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

14 of 40

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

15 of 40

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

16 of 40

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

17 of 40

Decision time!

Can I go to the movies? I’m allowed to if it’s before 8 o’clock.

Do This:

  • What information do I need to know?
  • Draw a model of a variable to store that information. Give it a name.

Unit 4 Lesson 5 - Activity

18 of 40

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

19 of 40

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

20 of 40

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

21 of 40

Unit 4 Lesson 5 - Warm Up

  • In your photo liker app, when would using a conditional statement be useful?

onEvent("downButton", "click", function( ) {

likes = likes - 1;

setText("likeCounter","Likes: " + likes);

});

22 of 40

Unit 4 Lesson 5 - Warm Up

  • In your photo liker app, when would using a conditional statement be useful?

onEvent("downButton", "click", function( ) {

if(numLikes>0)

{

numLikes = numLikes - 1;

}

setText("likeCounter","Likes: " + likes);

});

23 of 40

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

24 of 40

Can I adopt a cat?

I can if have 40 dollars or more

AND

I am over 14 years old

Do This:

  • What information do I need to know?
  • Draw a model of the variables to store that information. Give the variables names.
  • With a partner, discuss how the flowchart might be set up. What will the Boolean expression(s) look like? What will be compared?

Unit 4 Lesson 5 - Activity

25 of 40

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

26 of 40

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

27 of 40

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

28 of 40

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

29 of 40

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

30 of 40

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

31 of 40

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.

32 of 40

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

33 of 40

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

34 of 40

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

35 of 40

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

36 of 40

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

37 of 40

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

38 of 40

Key Takeaways

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

10

2

evaluates to

<

false

4

4

evaluates to

==

true

Unit 4 Lesson 5 - Activity

39 of 40

Key Takeaways

  • 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 simplified 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

true

false

evaluates to

&&

false

true

false

evaluates to

||

true

Unit 4 Lesson 5 - Activity

40 of 40

Homework:

  • Complete U4L05 goFormative Conditionals Explore
  • Submit image of today's notes and truth table of boolean values written in your IN to Hub

Reminder: Test (U3 and U4) on intro to programming - variables, strings/output, key words