Upload
anthony-hopkins
View
214
Download
0
Embed Size (px)
Citation preview
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
BOOlean Expressions
Examples:
‘a’ == ‘b’ (== not initiating variables)
2 == 3
‘a’ > ‘b’
‘a’ < ‘b’
10 >= 10
5 != 2
True or False
True or True =
True or False =
False or True =
False or False =
True or False
True and True =
True and False =
False and True =
False and False =
NOT!
not False =
not (True or False) =
not (False and False) =
not (True or True) =
DeMorgan’s Law
• not (P and Q) = not P or not Q
For example:
not (True and False) = not True or not False
If Conditional Statements
if RELATIONALEXPRESSION:
statement
•Lowercase if
•Colon after the RE
•Statements that follow must be indented
If Example:
if rg3 == ‘injured’:
print(‘RG3 is out for the season!”)
if rg3 != ‘quarterback’:
print(“RG3 is not the quarterback!”)
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!’)
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
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!”)
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?
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