relational expressions boolean logic conditionals · conditionals cs105 | conditionals 1 boolean...

57
Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16, 5-17, 5-18, 5-19, Robot 3 (p. 84-86).

Upload: others

Post on 24-Jun-2020

44 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Conditionals

CS105 | Conditionals 1

Boolean Logic

Relational Expressions

Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16, 5-17, 5-18, 5-19, Robot 3 (p. 84-86).

Page 2: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Recap: What is Algorithm? (cf. Lecture 02 Algorithms)

Algorithms typically …

… generate same output.

… make some assumptions.

… have multiple solutions.

… include decisions.

… are expressed in modular pieces.

CS 105 - Code 2

Page 3: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Recap: What is Algorithm? (cf. Lecture 02 Algorithms)

Algorithms typically …

… generate same output.

… make some assumptions.

… have multiple solutions.

… include decisions.

… are expressed in modular pieces.

CS 105 - Code 3

Page 4: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Decisions made in our everyday life

CS 105 - Code 4

▪ “What should I eat for lunch?”

▪ “Should I do the lab/assignment today or later?”

▪ “Can I cross the road?”

▪ … and more

Page 5: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

How do we decide?

CS 105 - Code 5

▪ “What should I eat for lunch?”

▪ “Should I do the lab/assignment today or later?”

▪ “Can I cross the road?”

▪ … and more

Page 6: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

How do we decide? (Decision-making process)

CS 105 - Code 6

CheckCondition

1

“Is today taco special day?”

“Is the deadline today?”

“Is it green light?”

Page 7: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

How do we decide? (Decision-making process)

CS 105 - Code 7

CheckCondition

1

“Is today taco special day?”

“Is the deadline today?”

“Is it green light?”

“Yes”

“No”

“Yes”

Page 8: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

How do we decide? (Decision-making process)

CS 105 - Code 8

CheckCondition

1

TakeAction

2

“Is today taco special day?”

“Is the deadline today?”

“Is it green light?”

“Let’s eat tacos!”

“Let’s do it tomorrow”

“Let’s cross the road”

“Yes”

“No”

“Yes”

Page 9: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

CS 105 - Code 9

More examples (Decision-making process)

Page 10: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

More examples (Decision-making process)

CS 105 - Code 10

Page 11: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

More examples (Decision-making process)

CS 105 - Code 11

Page 12: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

More examples (Decision-making process)

CS 105 - Code 12

CheckCondition

1

Page 13: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

More examples (Decision-making process)

CS 105 - Code 13

CheckCondition

1

TakeAction

2

Page 14: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Note: general decision-making process

CS 105 - Code 14

CheckCondition

1

TakeAction

2

Page 15: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

But why do we need programs to make decisions?

CS 105 - Code 15

Page 16: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

But why do we need programs to make decisions?

CS 105 - Code 16

▪ We began by studying...

- how to draw static image by learning about

- method, canvas, coordinates

- then started learning how to make it

dynamic & interactive using...

– draw() function

– variables (e.g., mouseX, circleSize, etc.)

– keyPressed() and mousePressed() functions

Page 17: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

But why do we need programs to make decisions?

CS 105 - Code 17

▪ We began by learning...

- how to draw static image by learning about

- method, canvas, coordinates

- then started learning how to make it

dynamic & interactive using...

– draw() function

– variables (e.g., mouseX, circleSize, etc.)

– keyPressed() and mousePressed() functions

Page 18: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

But why do we need programs to make decisions?

CS 105 - Code 18

▪ It allows us to do even more with our program

(in terms of making it more dynamic & interactive)

Page 19: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

19

Example

Page 20: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

20

Actually, we already saw our program make decisions

▪ Event functions follow the same decision-making process

- e.g., mousePressed(), keyPressed()

CheckCondition

1

TakeAction

2

“Did user just press mouse?”

“Did user just press mouse?”

“Run the code inside”

“Nothing”

“Yes”

“No”

function mousePressed() {

// decision

}

// do the following

Page 21: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

21

But we need user-defined events

▪ They are limited to keyPressed() and mousePressed() events

▪ We need to represent user-defined events, such as...

Page 22: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

22

But we need user-defined events

▪ They are limited to keyPressed() and mousePressed() events

▪ We need to represent user-defined events, such as...

CheckCondition

1

TakeAction

2

“Did pac-man lose all lives?”

“Did any ghost touch pac-man?”

“Did pac-man complete the stage?”

“Continue the game”

“Remove one life & restart”

“Move to next stage”

“No”

“Yes”

“Yes”

Page 23: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Boolean Logic

23

▪ It can help us define user-defined events

Page 24: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

CS 105 | Variables 24

The Genius of George Boole (1:43)https://youtu.be/aEjzjLv-YjI

Boolean Logic

Page 25: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

What is boolean logic?

25

▪ Based on an idea that some statement P is either true or false.

(It can’t be anything in between.)

▪ Boole suggested that this forms the basis of our thought process

P

(true)

(false)

Page 26: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

So how do we write boolean logic?

CS105 | Conditionals 26

P

(true)

(false)

Page 27: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Let’s first look at boolean expression, and then logic

CS105 | Conditionals 27

P

(true)

(false)

boolean logic

boolean expression

1

2

Page 28: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Boolean Expression (= some statement P)

CS105 | Conditionals 28

▪ An expression that evaluates to true or false.

P

(true)

(false)

boolean expression

Page 29: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Pick a number

CS105 | Conditionals 29

Page 30: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Boolean Expressions (i.e., some statement P)

CS105 | Conditionals 30

▪ Relational boolean expressions:- is your number greater than 50?- is your number less than 40?- is your number equal to 73?- is your number greater than or equal to 75?- is your number less than or equal to 37?- is your number not equal to 99?

P

(true)

(false)

Page 31: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Relational Operators

CS105 | Conditionals 31

is your number greater than 50?

number > 50is your number less than 40?

number < 40is your number equal to 73?

number === 73is your number greater than or equal to 75?

number >= 75is your number less than or equal to 37?

number <= 37is your number not equal to 99?

number !== 99

>

<

===

>=

<=

!==

Page 32: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

(demo)

CS105 | Conditionals 32

// my number to guesslet number = 73;

// a relational expression used to guess my numberprint(number > 50);

https://editor.p5js.org/sanghosuh/sketches/kgv_Tre1

Page 33: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

True or False?

CS105 | Conditionals 33

let a = 8;let b = 5;

6 < aa / 2 > 6a * 5 === 8 * bb !== 5

A. true

B. false

C. neither true or false

Page 34: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Now, let’s look at how to write boolean logic

CS105 | Conditionals 34

P

(true)

(false)

boolean logic2

boolean expression

1

Page 35: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Conditional Statement

CS105 | Conditionals 35

▪ if a boolean expression (P) is true, then execute a block of code

boolean expression

code block to execute if

true

coding

P

Page 36: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Conditional Statement

CS105 | Conditionals 36

▪ if a number is greater than 50, then execute a block of code

coding

Page 37: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Conditional Statement (Flowchart representation)

CS105 | Conditionals 37

▪ if a number is greater than 50, then execute a block of code

Page 38: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

animation

CS105 | Conditionals 38

using a conditional statement to “loop” an animation

https://editor.p5js.org/cs105/sketches/xkfOldfyI

Starter: https://editor.p5js.org/cs105/sketches/McJ4bsUNJ

Page 39: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

conditional_dot1 (if)

CS105 | Conditionals 39

// the conditional statementif (mouseX > width / 2) { fill("#FF0000"); // red ellipse(0.75 * width, 50, 30, 30);}

https://editor.p5js.org/cs105/sketches/DorsbS_Hk

Page 40: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Nested Code Blocks

CS105 | Conditionals 40

Page 41: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

see “08 Conditionals (trace)”

CS105 | Conditionals 41

Page 42: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

What can this code draw?

CS105 | Conditionals 42

function draw() { background(200); fill(0); // black

if (mouseX < 50) { fill(255); // white }

ellipse(mouseX, mouseY, 20);}

A B

C DA, B, and C are all possible

Page 43: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Gas Station Analogy (if statement)

CS105 | Conditionals 43

▪ A single if statement is ike deciding what to do at gas station- the if code block is something you do if a condition is true

(if tank is low, then get gas)- after checking this condition, you continue down the road …

Page 44: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

If statement (Flowchart representation)

44

code block to execute if

true

code block to execute if

false

Continue down the road

tank fuel is low

Page 45: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

conditional_dot2 (if else)

CS105 | Conditionals 45

// the conditional statementif (mouseX > width / 2) { fill("#FF0000"); // red ellipse(0.75 * width, 50, 30, 30);} else { fill("#0000FF"); // blue ellipse(0.25 * width, 50, 30, 30);}

https://editor.p5js.org/cs105/sketches/nuHTR21Ci

Page 46: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Gas Station Analogy (if else statement)

CS105 | Conditionals 46

▪ A if else statement is like deciding what to do at gas station- the if code block is something you do if a condition is true

(if tank is low, then get gas)- the else code block is something you do if a condition is false

(if tank has enough, then just get a snack)- after doing one of the two, you continue down the road ...

Page 47: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

If - else statement (Flowchart representation)

47

code block to execute if

true

code block to execute if

false

Continue down the road

tank fuel is low

Page 48: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

What can this code draw?

CS105 | Conditionals 48

function draw() { background(200);

if (mouseY > 50) { fill(0); // black } else { fill(255); // white }

ellipse(mouseX, mouseY, 20, 20);

}

A B

C DA, B, and C

are all possible

Page 49: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Gas Station Analogy (if - else if - else statement)

CS105 | Conditionals 49

▪ A if else if else statement is deciding what to do at gas station- the if code block is something you do if a condition is true

(if tank is low, then get gas)- the else if code block is something you do if a second condition is

true(if tank is not low but you’re hungry, grab a pizza)- the else code block is something you do if none of the above

conditions is true(if tank is not low and you’re not hungry, grab a coffee)- after doing one of the three, you will continue down the road

Page 50: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

If - else if - else statement (Flowchart representation)

50

else ifelse

tank fuel is low

I am hungry

Page 51: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

conditional_dot3 (if else if)

CS105 | Conditionals 51

…// the conditional statementif (mouseX > width / 2) { fill("#FF0000"); // red ellipse(0.75 * width, 50, 30, 30);} else if (mouseX < width / 2) { fill("#0000FF"); // blue ellipse(0.25 * width, 50, 30, 30);} else { background(random(255)); }…

https://editor.p5js.org/cs105/sketches/M2WPtcp1l

Page 52: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Two Common Logic Errors with if statements

CS105 | Conditionals 52

if (mouseX > 50); { fill("#FF0000"); // red ellipse(0.75 * width, 50, 30, 30);}

// the conditional statementif (mouseX > 50) { fill("#FF0000"); // red ellipse(0.75 * width, 50, 30, 30);} if (mouseX < 50) { fill("#0000FF"); // blue ellipse(0.25 * width, 50, 30, 30);} else { background(random(255));}

Adding semicolon after the boolean expression means no code block. A code block with no "if" means always execute.

Missing "else" makes two different (and independent) if statements.

Page 53: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

shaper

CS105 | Conditionals 53

checking what key was pressed:

if (key === 'c') { shape = 1;} else if (key === 'r') { shape = 2;} else if (key === 't') {…

using a variable to save 'state':

if (shape === 1) { ellipse( … ); } else if (shape === 2) { rect( … ); } else if (shape === 3) { triangle( … ); }

https://editor.p5js.org/cs105/sketches/d69-jjtUT

Page 54: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

random_select

CS105 | Conditionals 54

selecting a random shape:

let shape = 0;

if (shape === 1) { ellipse( … ); } else if (shape === 2) { rect( … ); } else if (shape === 3) { triangle( … ); }

shape = int(random(1, 4));

https://editor.p5js.org/sanghosuh/sketches/ftWomWsw

Page 55: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

JavaScript Equality Relational Operators

CS105 | Conditionals 55

▪ In JavaScript, there are two equality operators: == and ===- You should use === because it’s a strict comparison- Same goes for != and !== operators, use !==

(In other languages, there is only == and !=, and these are strict)

===

!==

==

!=https://codeahoy.com/javascript/2019/10/12/==-vs-===-in-javascript/

https://editor.p5js.org/sanghosuh/sketches/_rv8pQhb

Page 56: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

==

Equality vs. Assignment Operators

CS105 | Conditionals 56

Equality Operator- is the left value equal to the right value?- e.g. “state === 1” means "is state equal to 1?" - left and right can be anything that reduces to a

single value (a variable, a function that returns a value, a number, an expression)

Assignment Operator- assign the right side value to the variable on left- e.g. “state = 1” means “assign 1 to state”- left must be a variable, right can be anything that

reduces to a single value (a variable, a function that returns a value, a number, an expression)

===

=

Page 57: Relational Expressions Boolean Logic Conditionals · Conditionals CS105 | Conditionals 1 Boolean Logic Relational Expressions Chapter 5, Examples 5-10, 5-11, 5-12, 5-13, 5-15, 5-16,

Office Hours

CS105 | Conditionals

▪ My office hours are on Fridays 10:00 - 12:00 at DC2102

▪ If you want to make an appointment, you can sign up

using the following link:

https://calendly.com/sanghosuh/cs105-office-hours

▪ You do NOT have to make an appointment,

but then it will be on a first come, first served basis.

57