1 of 21

CSE 160

If Statements

Hannah Cheung

2 of 21

Example: Absolute value

  • Absolute value - distance of a number from zero
    • absolute value of 5 is 5
    • absolute value of 0 is 0
    • absolute value of -22 is 22
  • If value is negative, negate it.

Otherwise, use the original value.

2

3 of 21

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.

4 of 21

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.

5 of 21

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.

6 of 21

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.

7 of 21

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.

8 of 21

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.

9 of 21

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.

10 of 21

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

11 of 21

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.

12 of 21

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

13 of 21

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.

14 of 21

Example: Atmosphere

14

troposphere

stratosphere

mesosphere

space

20

0

50

100

km

15 of 21

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

16 of 21

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

17 of 21

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

18 of 21

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

19 of 21

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

20 of 21

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

21 of 21

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