14
Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

Embed Size (px)

Citation preview

Page 1: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

Lesson 6: Boolean and If Statements

Computer Science 1

Mr. Bernstein

Page 2: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

BOOlean

• Boolean are statements that yield a truth or false value.

• Works with Strings and Integers

Page 3: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

BOOlean Expressions

Examples:

‘a’ == ‘b’ (== not initiating variables)

2 == 3

‘a’ > ‘b’

‘a’ < ‘b’

10 >= 10

5 != 2

Page 4: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

True or False

True or True =

True or False =

False or True =

False or False =

Page 5: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

True or False

True and True =

True and False =

False and True =

False and False =

Page 6: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

NOT!

not False =

not (True or False) =

not (False and False) =

not (True or True) =

Page 7: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

DeMorgan’s Law

• not (P and Q) = not P or not Q

For example:

not (True and False) = not True or not False

Page 8: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

If Conditional Statements

if RELATIONALEXPRESSION:

statement

•Lowercase if

•Colon after the RE

•Statements that follow must be indented

Page 9: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

If Example:

if rg3 == ‘injured’:

print(‘RG3 is out for the season!”)

if rg3 != ‘quarterback’:

print(“RG3 is not the quarterback!”)

Page 10: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

IF Examples:

rgcondition = ‘good condition’

qb = ‘rg3’

if rgcondition == ‘injured’:

print(“RG3 is not playing next Sunday.”)

if rgcondition != ‘injured’ and qb == ‘rg3’:

print(‘RG3 is playing this Sunday!’)

Page 11: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

All together!

Write a program that prompts the user how many hours they work. (input)

If they work 40 hours or less, they make 8 dollars an hour. (if statement)

If they work more than 40 hours, each hour after they make 10 dollars an hour.

(if statement)

Determine how much a person makes if they work: 21 hours, 40 hours, 48 hours

Page 12: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

Else and elif Statement

No need for multiple if statements!

if ( apple > 10):

print(“I has more apples!”)

elif (apple <= 10):

print(“I has less apples :-( “)

else:

print(“I don’t know how many I have!”)

Page 13: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

Example in ClassWrite me a program that tells me what is for lunch when the user inputs a day of the week. (prompt the user)

Monday – Sloppy Joes

Wednesday – Chicken Patties

Friday - Salads

Tues/Thurs – Pizza

(challenge) What if someone inputs Sat/Sun? What if they don’t input a day of the week?

Page 14: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein

Summary

• Boolean – True or False

• If statement– RelationalExpression: (colon)

<Indent> statement

If, Elif, and Else – Conditions that aren’t easily put into an if statement