lesson 6: boolean and if statements computer science 1 mr. bernstein
TRANSCRIPT
![Page 1: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein](https://reader036.vdocuments.net/reader036/viewer/2022082917/55140507550346e7488b4aa6/html5/thumbnails/1.jpg)
Lesson 6: Boolean and If Statements
Computer Science 1
Mr. Bernstein
![Page 2: Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein](https://reader036.vdocuments.net/reader036/viewer/2022082917/55140507550346e7488b4aa6/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082917/55140507550346e7488b4aa6/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082917/55140507550346e7488b4aa6/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082917/55140507550346e7488b4aa6/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082917/55140507550346e7488b4aa6/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082917/55140507550346e7488b4aa6/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082917/55140507550346e7488b4aa6/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082917/55140507550346e7488b4aa6/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082917/55140507550346e7488b4aa6/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082917/55140507550346e7488b4aa6/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082917/55140507550346e7488b4aa6/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082917/55140507550346e7488b4aa6/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022082917/55140507550346e7488b4aa6/html5/thumbnails/14.jpg)
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