1 of 19

If Statements

Winter 2025

1

Adrian Salguero

2 of 19

Announcements

  • Coding Practice 1 and Homework 1 both due Friday, January 17th at 11:59pm
  • Please notify me of any Religious Accommodations you will need by the end of this Friday, January 17th.

2

3 of 19

What we know so far

  • Expressions

3 + 4

(8 / 3) + 2

  • Variables

x = 7

y = x * 2

  • Print statements

print(“Hello”)--> Hello

print(“Hello ” + “World” + “!”) → Hello World!

3

Types

True (bool)

42 (int)

“Hello” (string)

4.2 (float)

4 of 19

What we know so far

  • For loops

for letter in "hello":

print(letter)

  • Nested For Loops

for ones in range(5):

for tens in range(10):

print(tens * 10 + ones + 1)

  • Lists

[1, 2, 3]

range(10)-> [1, 2, 3, 4, 5, 6, 7, 8, 9]

4

5 of 19

Making decisions

  • How do we compute absolute value?

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.

6 of 19

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

7 of 19

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:

8 of 19

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)

9 of 19

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)

10 of 19

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.

11 of 19

Only if

It is not required that anything happens

11

val = -10

if val < 0:

print("negative value!")

What happens when val = 5?

12 of 19

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)

13 of 19

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

14 of 19

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

15 of 19

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

16 of 19

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

17 of 19

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

18 of 19

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

19 of 19

Todos for next lecture

  • Continue working on Homework 1 and Coding Practice 1
  • Attend section tomorrow
  • Ask questions on Ed or come to office hours

19