08 If Statements
08 If Statements
Online Course
Supported by
* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational materials including the video and other
relevant materials for commercial and for-profit use.
If Statement
08
Global Leadership School
Handong Global University
Learning Objectives
• Learn when to use conditionals
• If statement
• if
• if ~ else
• if ~ elif
N Condition Y
?
statements
How to use if statement
• If statements are made of a “block” that contains
a conditional clause and execution statement
• Colon must always follow after a condition
• Executed statements must be written on the next line
• The execution statement can be one or multiple lines
• If the execution statement is multi-line, the
indentation must match
• Indented executed statements that come after a
conditional statement is called “block”
• The block of a conditional is also called its body
if Statement, Example
>>> count = 10
>>> if count >=10 : ## Ends with colon
count = 0
Indent print(“=“ * 25)
print(“Restart from the beginning”)
print(“=“ * 25)
Y Condition N
?
statement1 statement2
If ~ else Statement, Example
count = 10
if count >=10 :
count = 0
print("=" * 25)
print(”Restart from beginning")
print("=" * 25)
else :
count = count + 1
print(”Proceeding to next stage")
If ~ else Statement
if x%2 == 0 :
print( x, "is even“)
else :
print( x, "is odd“)
# Simple comparison
if a < b :
print("a is less than b")
if a > b :
print("a is greater than b")
if a <= b :
print("a is less than or equal to b")
else :
print("a is greater than b")
if a == b :
print("a is equal to b")
else :
print("a and b are not equal")
If ~ elif Statement
• Used when a condition can be divided into
more than two cases if condition :
statement
• Conditions are divided into sections elif condition :
• Grades, commission rate in real-estate statement
…
• Conditions are listed sequentially elif condition :
• elif short for “else if” statement
else :
• Can use multiple elif in the if statement statement
• Exactly one block is executed
• Else is only used for the final block
If ~ elif Statement
x=5 • If x is less than y
y = 10
• ‘5 is less than 10’
if x < y :
print( x, "is less than", y) • If x is greater than y
elif x > y :
print( x, "is greater than", y) • ‘10 is greater than 5’
else :
print( x, "and", y, “is equal to“)
• If x is equal to y
• ‘5 is equal to 5’
If ~ elif Statement, Example(1)
# Enter Grade
score = 75
if score >= 90 :
print(“Your grade is A”)
elif score >=75:
print(“Your grade is B”)
elif score >=60:
print(“Your grade is C”)
else:
print(“Your grade is F”)
If ~ elif Statement, Example(2)
# brokers commission
amount= int(input(“Enter your transaction amount : “))
if fruit_input in fruits :
print(”The fruit is in the list.")
else :
print(”The fruit is not in the list.")
Nested conditional statements
• Conditional statement can be used in other
conditional statements
if x == y :
print (x, "and", y, "are equal“)
else :
if x < y :
print( x, "is less than", y)
else :
print( x, "is greater than", y)
# operator and
if temperature > 35 :
print(”It’s very hot")
elif temperature > 30 :
print(”It’s hot")
Lecture Summary
• When to use an conditional statement
• If the output of a program should change depending on
the conditions
• Conditional Statement
• if statement: If the condition is true
• if ~ else statement: Ran either condition is true and false
• if ~ elif statement: Ran depending on the condition
* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational
materials including the video and other relevant materials for commercial and for-profit use.
CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik.