If Statements
Winter 2025
1
Adrian Salguero
Announcements
2
What we know so far
3 + 4
(8 / 3) + 2
x = 7
y = x * 2
print(“Hello”)--> Hello
print(“Hello ” + “World” + “!”) → Hello World!
3
Types
True (bool)
42 (int)
“Hello” (string)
4.2 (float)
What we know so far
for letter in "hello":
print(letter)
for ones in range(5):
for tens in range(10):
print(tens * 10 + ones + 1)
[1, 2, 3]
range(10)-> [1, 2, 3, 4, 5, 6, 7, 8, 9]
4
Making decisions
Absolute value of 5 is
Absolute value of 0 is
Absolute value of -22 is
5
If the value is negative, negate it.
Otherwise, use the original value.
Absolute value solution
If the value is negative, negate it.
Otherwise, use the original value.
6
val = -10
# calculate absolute value of val
if val < 0:
result = -val
else:
result = val
print(result)
In this example, result will always be assigned a value.
Condition must be a Boolean expression
Indentation is significant
else is not required
Absolute value solution
If the value is negative, negate it.
Otherwise, use the original value.
7
val = -10
# calculate absolute value of val
if val < 0:
result = -val
else:
result = val
print(result)
val = -10
if val < 0:
print(-val)
else:
print(val)
Another approach �that does the same thing�without using result:
Another Absolute value solution
As with loops, multiple statements can be (and often are)
used in place of a single statement:
8
val = -10
# calculate absolute value of val
if val < 0:
result = -val
print("val is negative!")
print("I had to do extra work!")
else:
result = val
print("val is positive")
print(result)
Nested if statements
9
val = 0
# calculate absolute value of val
if val < 0:
print("val is negative")
print(val)
result = -val
else:
if val == 0:
print("val is zero")
print(val)
result = val
else:
print("val is positive")
print(val)
result = val
print(result)
Use elif instead of nested ifs
10
val = 0
# calculate absolute value of val
if val < 0:
print("val is negative")
print(val)
result = -val
elif val == 0:
print("val is zero")
print(val)
result = val
else:
print("val is positive")
print(val)
result = val
print(result)
Easier to read than nested if.�Equivalent to code on previous slide.
Only if
It is not required that anything happens…
11
val = -10
if val < 0:
print("negative value!")
What happens when val = 5?
Another absolute value solution
What happens here?
12
val = 5
# calculate absolute value of val
if val < 0:
result = -val
print("val is negative!")
else:
for i in range(val):
print("val is positive!")
result = val
print(result)
The if body can be any statements
# height is in km
if height > 100:
print("space")
else:
if height > 50:
print("mesosphere")
else:
if height > 20:
print("stratosphere")
else:
print("troposphere")
13
Code gets here only when “height > 100” is false
AND “height > 50” is true
else�clause
i
e
i
e
0
10
20
30
40
50
60
70
80
90
100
troposphere
stratosphere
mesosphere
space
km�above�earth
Code gets here only when “height > 100” is false
if�clause
Version 1
# height is in km
if height > 100:
print("space")
else:
if height > 50:
print("mesosphere")
else:
if height > 20:
print("stratosphere")
else:
print("troposphere")
14
0
10
20
30
40
50
60
70
80
90
100
troposphere
stratosphere
mesosphere
space
km�above�earth
Version 2
15
if height > 50:
if height > 100:
print("space")
else:
print("mesosphere")
else:
if height > 20:
print("stratosphere")
else:
print("troposphere")
0
10
20
30
40
50
60
70
80
90
100
troposphere
stratosphere
mesosphere
space
km�above�earth
Version 3 (Best)
if height > 100:
print("space")
elif height > 50:
print("mesosphere")
elif height > 20:
print("stratosphere")
else:
print("troposphere")
16
ONE of the print statements is guaranteed to execute: �
whichever true condition it encounters first
0
10
20
30
40
50
60
70
80
90
100
troposphere
stratosphere
mesosphere
space
km�above�earth
Order Matters
# version 3
if height > 100:
print("space")
elif height > 50:
print("mesosphere")
elif height > 20:
print("stratosphere")
else:
print("troposphere")
# broken version 3
if height > 20:
print("stratosphere")
elif height > 50:
print("mesosphere")
elif height > 100:
print("space")
else:
print("troposphere")
17
Try height = 72 on both versions, what happens?
0
10
20
30
40
50
60
70
80
90
100
troposphere
stratosphere
mesosphere
space
km�above�earth
Incomplete Version 3
# incomplete version 3
if height > 100:
print("space")
elif height > 50:
print("mesosphere")
elif height > 20:
print("stratosphere")
18
In this case it is possible that nothing is printed at all, when?
0
10
20
30
40
50
60
70
80
90
100
troposphere
stratosphere
mesosphere
space
km�above�earth
Todos for next lecture
19