Search Header Logo
Conditionals(If and elif)

Conditionals(If and elif)

Assessment

Presentation

Computers

1st - 5th Grade

Practice Problem

Hard

Created by

Sakxam Shrestha

Used 3+ times

FREE Resource

34 Slides • 0 Questions

1

media

Conditionals

American Baptist College

2

media

Review: Comparison Operators

What are they?

Operators comparing values to return boolean value

Why are they important?

Compare values and make decisions based on evaluated outcome

Examples

<

<=

==

>

>=

!=

less than

less than or equal to

greater than or equal to

greater than

equal to

not equal to

3

media

What are they?

Operators that combine booleans

and: True only if both operands are True

or: True if either (or both) operand is True

not: Inverts value of its operand

order of operations: PNAO

Why are they important?

Enable programs to make decisions based on multiple conditions

Review: Logical Operators

4

media

What are they?

Why are they important?

Examples

Objective: Conditionals

5

media
media

Is order over

$25?
yes

Shipping is free!

no

Shipping is $5.99

6

media

Making Decisions

conditional: control structure (specify flow) to run code
block only if a certain condition is met

condition: expression that evaluates to boolean value

Conditionals are often referred to as if statements

7

media
media

order > 25

otherwise

Free shipping!

$5.99 shipping

Condition

Conditional

8

media

if order > 25:

shipping = 0.0

else:

shipping = 5.99

If Statement: A Preview

9

media

if [statement]:
[execute this code]

If statements

if this STATEMENT evaluates
to True

10

media

If statements

if [statement]:
[execute this code]

run the code that is INDENTED
after if statement

11

media

If statements

if [statement]:
[execute this code]

Indent is important! It's how
program knows that this code
"belongs" to this if statement.

12

media

If statements

if [statement]:
[execute this code]
[execute this code]

If there are two lines of indented
code, both lines "belong" to if
statement.

13

media

If statements

if [statement]:
[execute this code]
[execute this code]
else:
[execute this code]

Specifies to do something if
STATEMENT above is FALSE

14

media

If statements

if [statement]:
[execute this code]
[execute this code]
else:
[execute this code]

Only runs when STATEMENT
above is FALSE

15

media

order = 30

if order > 25:

shipping = 0.0

else:

shipping = 5.99

print('Total: ' + str(order + shipping))

True

skip

16

media

What if order isn’t enough for free shipping?

17

media

order = 20

if order > 25:

shipping = 0.0

else:

shipping = 5.99

print('Total: ' + str(order + shipping))

False

run this!

18

media

fruits = 5

if fruits / 2 < 2:

print('apples')

else:

if fruits % 2 == 1:

print('bananas')

else:

print('oranges')

Practice

Outer if statement

19

media

fruits = 5

if fruits / 2 < 2:

print('apples')

else:

if fruits % 2 == 1:

print('bananas')

else:

print('oranges')

Practice

Nested (inner) if statement

20

media

fruits = 5

if fruits / 2 < 2:

print('apples')

else:

if fruits % 2 == 1:

print('bananas')

else:

print('oranges')

Practice

Add another nested if statement?

21

media

fruits = 5
if fruits / 2 < 2:
print('apples')
else:
if fruits % 2 == 1:
print('bananas')
else:
if fruits + 2 == 10:
print('pineapple')

else:

print('oranges')

Practice

22

media

fruits = 5
if fruits / 2 < 2:
print('apples')
else:
if fruits % 2 == 1:
print('bananas')
else:
if fruits + 2 == 10:
print('pineapple')

else:

print('oranges')

Practice

And another?

23

media

fruits = 5
if fruits / 2 < 2:
print('apples')
else:
if fruits % 2 == 1:
print('bananas')
else:
if fruits + 2 == 10:
print('pineapple')

else:

if fruits * 2 == 36:
print('pear')

else:

print(oranges')

Practice

Yikes!
Becomes really difficult to read and follow

24

media

fruits = 5

if fruits / 2 < 2:

print('apples')

else:

if fruits % 2 == 1:

print('bananas')

else:

print('oranges')

Elif to the rescue!

fruits = 5

if fruits / 2 < 2:

print('apples')

elif fruits % 2 == 1:

print('bananas')

else:

print('oranges')

25

media

fruits = 5
if fruits / 2 < 2:
print('apples')
else:
if fruits % 2 == 1:
print('bananas')
else:
if fruits + 2 == 10:
print('pineapple')

else:

if fruits * 2 == 36:
print('pear')

else:

print(oranges')

Elif to the rescue!

fruits = 5
if fruits / 2 < 2:
print('apples')
else:
if fruits % 2 == 1:
print('bananas')
else:
if fruits + 2 == 10:
print('pineapple')

elif fruits * 2 == 36:

print('pear')

else:

print(oranges')

26

media

fruits = 5
if fruits / 2 < 2:
print('apples')
else:
if fruits % 2 == 1:
print('bananas')
else:
if fruits + 2 == 10:
print('pineapple')

elif fruits * 2 == 36:

print('pear')

else:

print(oranges')

Elif to the rescue!

fruits = 5
if fruits / 2 < 2:
print('apples')
else:
if fruits % 2 == 1:
print('bananas')
elif fruits + 2 == 10:
print('pineapple')
elif fruits * 2 == 36:
print('pear')
else:

print(oranges')

27

media

fruits = 5
if fruits / 2 < 2:
print('apples')
else:
if fruits % 2 == 1:
print('bananas')
elif fruits + 2 == 10:
print('pineapple')
elif fruits * 2 == 36:
print('pear')
else:

print(oranges')

Elif to the rescue!

fruits = 5
if fruits / 2 < 2:
print('apples')
elif fruits % 2 == 1:
print('bananas')
elif fruits + 2 == 10:
print('pineapple')
elif fruits * 2 == 36:
print('pear')
else:
print(oranges')

28

media

fruits = 5
if fruits / 2 < 2:
print('apples')
else:
if fruits % 2 == 1:
print('bananas')
else:
if fruits + 2 == 10:
print('pineapple')

else:

if fruits * 2 == 36:
print('pear')

else:

print(oranges')

Elif to the rescue!

fruits = 5
if fruits / 2 < 2:
print('apples')
elif fruits % 2 == 1:
print('bananas')
elif fruits + 2 == 10:
print('pineapple')
elif fruits * 2 == 36:
print('pear')
else:
print(oranges')

29

media

fruits = 5

if fruits / 2 < 2:

print('apples')

elif fruits % 2 == 1:

print('bananas')

elif fruits + 2 == 10:

print('pineapple')

elif fruits * 2 == 36:

print('pear')

else:

print(oranges')

Keeps going until condition is true
Executes code inside
Then stops

Elif to the rescue!

30

media

If / elif / else: what code will run?

if or elif blocks execute (aka expression evaluates to True), then no other
code will execute (else will NOT execute)

else will only execute when NONE of the if and elif expressions evaluate to
True (aka they all evaluate to False)

31

media

if fruits / 2 < 2:

print('apples')

elif fruits % 2 == 1:

print('bananas')

elif fruits + 2 == 10:

print('pineapple')

elif fruits * 2 == 36:

print('pear')

else:

print(oranges')

Elif to the rescue!

True

skip

skip

skip

skip

fruits = 1

32

media

if fruits / 2 < 2:

print('apples')

elif fruits % 2 == 1:

print('bananas')

elif fruits + 2 == 10:

print('pineapple')

elif fruits * 2 == 36:

print('pear')

else:

print(oranges')

Elif to the rescue!

False

True

skip

skip

skip

fruits = 5

33

media

if fruits / 2 < 2:

print('apples')

elif fruits % 2 == 1:

print('bananas')

elif fruits + 2 == 10:

print('pineapple')

elif fruits * 2 == 36:

print('pear')

else:

print(oranges')

Elif to the rescue!

False

False

False

False

Do this because all other
conditions were False

fruits = 10

34

media

Review: Conditionals

What are they?

if, elif, and else used to make decisions

Why are they important?

Allow for different behaviors based on conditions

media

Conditionals

American Baptist College

Show answer

Auto Play

Slide 1 / 34

SLIDE