CSE 160
If Statements
Hannah Cheung
Example: Absolute value
Otherwise, use the original value.
2
Example: Absolute value solution
val = -10
if val < 0:
result = -val
else:
result = val
print(result)
3
If value is negative, negate it.
Otherwise, use the original value.
Example: Absolute value solution
val = -10
if val < 0:
result = -val
else:
result = val
print(result)
4
condition must be a boolean expression
If value is negative, negate it.
Otherwise, use the original value.
Example: Absolute value solution
val = -10
if val < 0:
result = -val
else:
result = val
print(result)
5
condition must be a boolean expression
colon required
If value is negative, negate it.
Otherwise, use the original value.
Example: Absolute value solution
val = -10
if val < 0:
result = -val
else:
result = val
print(result)
6
condition must be a boolean expression
colon required
indent statements in conditional body
If value is negative, negate it.
Otherwise, use the original value.
Example: Absolute value solution
val = -10
if val < 0:
result = -val
else:
result = val
print(result)
7
condition must be a boolean expression
colon required
indent statements in conditional body
If value is negative, negate it.
Otherwise, use the original value.
In this example, result will always be assigned a value.
Example:
(Another) Absolute value solution
val = -10
if val < 0:
print(-val)
else:
print(val)
8
If value is negative, negate it.
Otherwise, use the original value.
Has same behavior, does not use result variable.
Example:
Multi-line Absolute value solution
val = -10
if val < 0:
result = -val
print(“val is negative”)
else:
result = val
print(“val is positive”)
9
Similar to loops, the conditional statement body can be more than one line.
Nested if statements
val = -10
if val < 0:
result = -val
print(“val is negative”)
else:
if val == 0:
result = val
print(“val is zero”)
else:
result = val
print(“val is positive”)
print(result)
10
added behavior when val == 0
elif
val = -10
if val < 0:
result = -val
print(“val is negative”)
elif val == 0:
result = val
print(“val is zero”)
else:
result = val
print(“val is positive”)
print(result)
11
Easier to read than nested if. Equivalent to code on previous slide.
Incomplete printing
val = -10
if val < 0:
result = -val
print(“val is negative”)
elif val == 0:
result = val
print(“val is zero”)
print(result)
12
Only if
val = -10
if val < 0:
print(“val is negative”)
13
An if statement does not need a following elif or else statement.
It is not required that anything happens if the condition is not met.
Example: Atmosphere
14
troposphere
stratosphere
mesosphere
space
20
0
50
100
km
Example: Atmosphere solution
if height > 100:
print(“space”)
else:
if height > 50:
print(“mesosphere”)
else:
if height > 20:
print(“stratosphere”)
else:
print(“troposphere”)
15
troposphere
stratosphere
mesosphere
space
20
0
50
100
km
Example: Atmosphere solution
if height > 100:
print(“space”)
else:
if height > 50:
print(“mesosphere”)
else:
if height > 20:
print(“stratosphere”)
else:
print(“troposphere”)
16
troposphere
stratosphere
mesosphere
space
20
0
50
100
km
height <= 100 is true here
Example: Atmosphere solution
if height > 100:
print(“space”)
else:
if height > 50:
print(“mesosphere”)
else:
if height > 20:
print(“stratosphere”)
else:
print(“troposphere”)
17
troposphere
stratosphere
mesosphere
space
20
0
50
100
km
height <= 100 is true here
height <= 100 and height > 50 here
Example:
Another Atmosphere solution
if height > 50:
if height > 100:
print(“space”)
else:
print(“mesosphere”)
else:
if height > 20:
print(“stratosphere”)
else:
print(“troposphere”)
18
troposphere
stratosphere
mesosphere
space
20
0
50
100
km
Example:
Best Atmosphere solution
if height > 100:
print(“space”)
elif height > 50:
print(“mesosphere”)
elif height > 20:
print(“stratosphere”)
else:
print(“troposphere”)
19
troposphere
stratosphere
mesosphere
space
20
0
50
100
km
Order matters
if height > 100:
print(“space”)
elif height > 50:
print(“mesosphere”)
elif height > 20:
print(“stratosphere”)
else:
print(“troposphere”)
if height > 20:
print(“stratosphere”)
elif height > 50:
print(“mesosphere”)
elif height > 100:
print(“space”)
else:
print(“troposphere”)
20
troposphere
stratosphere
mesosphere
space
20
0
50
100
km
Bugs in conditional statements
speed = 59
limit = 60
if speed <= limit:
print(“Good job!”)
else:
print(“You owe $”, speed / fine)
21
troposphere
stratosphere
mesosphere
space
20
0
50
100
km