

Conditionals(If and elif)
Presentation
•
Computers
•
1st - 5th Grade
•
Practice Problem
•
Hard
Sakxam Shrestha
Used 3+ times
FREE Resource
34 Slides • 0 Questions
1
Conditionals
American Baptist College
2
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
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
What are they?
Why are they important?
Examples
Objective: Conditionals
5
Is order over
$25?
yes
Shipping is free!
no
Shipping is $5.99
6
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
order > 25
otherwise
Free shipping!
$5.99 shipping
Condition
Conditional
8
if order > 25:
shipping = 0.0
else:
shipping = 5.99
If Statement: A Preview
9
if [statement]:
[execute this code]
If statements
if this STATEMENT evaluates
to True
10
If statements
if [statement]:
[execute this code]
run the code that is INDENTED
after if statement
11
If statements
if [statement]:
[execute this code]
Indent is important! It's how
program knows that this code
"belongs" to this if statement.
12
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
If statements
if [statement]:
[execute this code]
[execute this code]
else:
[execute this code]
Specifies to do something if
STATEMENT above is FALSE
14
If statements
if [statement]:
[execute this code]
[execute this code]
else:
[execute this code]
Only runs when STATEMENT
above is FALSE
15
order = 30
if order > 25:
shipping = 0.0
else:
shipping = 5.99
print('Total: ' + str(order + shipping))
True
skip
16
What if order isn’t enough for free shipping?
17
order = 20
if order > 25:
shipping = 0.0
else:
shipping = 5.99
print('Total: ' + str(order + shipping))
False
run this!
18
fruits = 5
if fruits / 2 < 2:
print('apples')
else:
if fruits % 2 == 1:
print('bananas')
else:
print('oranges')
Practice
Outer if statement
19
fruits = 5
if fruits / 2 < 2:
print('apples')
else:
if fruits % 2 == 1:
print('bananas')
else:
print('oranges')
Practice
Nested (inner) if statement
20
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Review: Conditionals
What are they?
if, elif, and else used to make decisions
Why are they important?
Allow for different behaviors based on conditions
Conditionals
American Baptist College
Show answer
Auto Play
Slide 1 / 34
SLIDE
Similar Resources on Wayground
23 questions
Computer Science preparatory
Presentation
•
1st - 5th Grade
28 questions
Elements of Poetry and Figurative Language
Presentation
•
1st - 5th Grade
25 questions
Digital Citizenship - Our Online Tracks
Presentation
•
3rd - 5th Grade
23 questions
szyfrowanie Cezar
Presentation
•
KG
26 questions
Slope Intercept Form Review
Presentation
•
KG
27 questions
Pictographs
Presentation
•
1st - 5th Grade
25 questions
ĐỀ NGHIỆM THU CUỐI NĂM (ĐỀ SỐ 02)
Presentation
•
KG
25 questions
EMBAJADORES SERVICE BOOKING CONTACT CENTER
Presentation
•
KG
Popular Resources on Wayground
24 questions
PBIS-HGMS Day 10
Quiz
•
6th - 8th Grade
10 questions
HCS SCI 03 Summer School Review 3
Quiz
•
3rd Grade
11 questions
Home Scope
Quiz
•
7th - 8th Grade
15 questions
HCS SCI 05 Summer School Assessment 3 Review
Quiz
•
5th Grade
35 questions
Lufkin Road Middle School Student Handbook & Policies Assessment
Quiz
•
7th Grade
18 questions
Geo 11.3 Area of Circles and Sectors
Quiz
•
9th - 11th Grade