© A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a...
Transcript of © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a...
![Page 1: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/1.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 2: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/2.jpg)
© A+ Computer Science - www.apluscompsci.com
If statements allow you to run code only if a condition is met.
num = 97if num >= 90:
print "Hello"print "World"
OUTPUTHello
World
![Page 3: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/3.jpg)
© A+ Computer Science - www.apluscompsci.com
If statements will skip the code if the condition is not met.
num = 50if num >= 90:
print "Hello"print "World"
OUTPUTWorld
![Page 4: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/4.jpg)
© A+ Computer Science - www.apluscompsci.com
Code inside an if statements must be indented
num = 97if num >= 90:
print "Hello"print "World"
OUTPUTHello
World
![Page 5: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/5.jpg)
© A+ Computer Science - www.apluscompsci.com
Relational operators compare two values and return True or False
num = 150print (num <= 90)
OUTPUTFalse
![Page 6: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/6.jpg)
© A+ Computer Science - www.apluscompsci.com
Relationalfrequently used operators
Name Use
== Determines if two values are equal
!= Determines if two values are not equal
> Determines if a value is greater than another
< Determines if a value is less than another
>= Determines if a value is greater than or equal to another
<= Determines if a value is less than or equal to another
![Page 7: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/7.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 8: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/8.jpg)
© A+ Computer Science - www.apluscompsci.com
If-Else statements allow you to execute different code if the statement is false
seconds = 10if seconds >= 60:
print "Over a minute"else:
print "Less than a minute"
OUTPUTLess than a minute
![Page 9: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/9.jpg)
© A+ Computer Science - www.apluscompsci.com
OUTPUTOver a minute
seconds = 90
if seconds >= 60:
print "Less than a minute" print "Over a minute"
True
![Page 10: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/10.jpg)
© A+ Computer Science - www.apluscompsci.com
OUTPUTLess than a minute
seconds = 10
if seconds >= 60:
print "Less than a minute" print "Over a minute"
False
![Page 11: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/11.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 12: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/12.jpg)
© A+ Computer Science - www.apluscompsci.com
seconds = 100 if seconds < 60:
print "Less than a minute"elif seconds < 120:
print "Over a minute“else:
print "Over two minutes"
OUTPUTOver a minute
If-elif statements allows you to check multiple conditions to determine which code to execute
![Page 13: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/13.jpg)
© A+ Computer Science - www.apluscompsci.com
OUTPUTOver a minute
seconds = 100
if seconds < 60:
elif seconds < 120:
print "Less than a minute"False
print "Over a minute"print "Over two minutes"
True
![Page 14: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/14.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 15: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/15.jpg)
© A+ Computer Science - www.apluscompsci.com
Logical operators allows you to check multiple conditions in the same if statement
x = 50y = 100if x < 400 and y < 300:print "Upper right"
elif x < 400 and y > 300:print "Upper left"
else:print "Bottom"
OUTPUTUpper right
![Page 16: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/16.jpg)
© A+ Computer Science - www.apluscompsci.com
Relationalfrequently used operators
Name Use
x or y Either x or y must be true
x and y Both x and y must be true
not x If x is true, it becomes false
If x is false, it becomes true
![Page 17: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/17.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 18: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/18.jpg)
© A+ Computer Science - www.apluscompsci.com
If statements can be put inside other if statements
OUTPUT
Passing
A
grade = 97if grade >= 70:print "Passing"if grade >= 90:
print “A"else:print "Failing"
![Page 19: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/19.jpg)
© A+ Computer Science - www.apluscompsci.com
The else part of and if-else statement MUST be in line with it’s corresponding if
OUTPUT
Passing
Failing
grade = 75if grade >= 70:print "Passing"if grade >= 90:
print “A"else:print "Failing"
![Page 20: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/20.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 21: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/21.jpg)
© A+ Computer Science - www.apluscompsci.com
The code in the lab runner files use nested ifs and elifs to check which key is pressed
![Page 22: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/22.jpg)
© A+ Computer Science - www.apluscompsci.com
if event.type==QUIT or (event.type==KEYUP and event.key==K_ESCAPE):
sys.exit()
elif event.type==KEYUP:if event.key==K_UP:keys = "Up Key"
elif event.key==K_DOWN:keys = "Down Key"
elif event.key==K_LEFT:keys = "Left Key"
elif event.key==K_RIGHT:keys = "Right Key"
![Page 23: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/23.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 24: © A+ Computer Science - ...© A+ Computer Science - If statements allow you to run code only if a condition is met. num = 97 if num >= 90: print "Hello" print "World" © A+ ...](https://reader033.fdocuments.net/reader033/viewer/2022052104/603edc97fa466f6e09759ca2/html5/thumbnails/24.jpg)
© A+ Computer Science - www.apluscompsci.com