© a+ computer science - ...© a+ computer science - if statements allow you to run code only if a...

24
© A+ Computer Science - www.apluscompsci.com

Upload: others

Post on 10-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© 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+

© A+ Computer Science - www.apluscompsci.com