Lesson-4.-USING-IF-ELIF-ELSE-STATEMENTS
Lesson-4.-USING-IF-ELIF-ELSE-STATEMENTS
Lesson
4
Using
If-Elif-Else
Statements
UNIT 3: Performing Conditional
Programming Using Python
If-Elif-Else Conditional
Structure
evaluates multiple conditions one at a
time.
If the current condition is true, it disregards the
remaining conditions. However, if the current
condition is false, it checks the next condition
and so on. Given this, it is necessary to
arrange our conditions in logical order. If all
conditions are false, the program executes the
statements found in else.
True
Condition Statement/s
False
True
Condition Statement/s
False
else Statements
In Python, its syntax is:
if condition:
statement/s
elif condition:
statement/s
else:
statement/s
The if-elif-else syntax also starts with the if.
When adding another condition, we use
elif.
The if block has only one (1) else as its last
option. However, the if block may also end
with an elif The colons () are always placed
next to the conditions and else.
Example 1: Score Remarks
Let us create a program that
asks the user to enter his or her
score in a 10-point quiz. The
program displays the remarks "Score
is out of range.". "You got a perfect
score", "You passed the quiz!", or
"Oops. Do better next time."
Pseudocode:
1. Assign the passing score to a variable.
2. Display the instruction to the user to enter his or her
score.
3. Accept the entered score and convert it into a float.
4. Evaluate if the entered score is greater than the
perfect score.
O Display "Score is out of range." if the score is
beyond
the perfect score.
5. If false, evaluate if the entered score is equal to the
6. If false, evaluate if the entered score is greater than
the
passing score.
O Display "You passed the quiz!" if the score
passed.
7. Else, display "Oops. Do better next time."
Flowchart
Start Yes Output “You
If score =
hps?
got a perfect
score!”
hps = 10
passingscore = 7.5 No
hps = 10
passingscore = 7.5
score = float(input(“please enter your score: “))
if score > hps:
print(“Score is out of range.”)
elif score ==hps:
print(“You got a perfect score!”)
elif score >= passingscore:
print(“You passed the quiz!”)
else:
print(“Oops. O better next time.”)
o The first two (2) line codes have comments, stating the
title and description of the program.
o The next two (2) lines have the variables and their
values. The hps variable has 10 This is for the highest
possible score of the quiz. The passingScore variable has
7.5.
o The assignment operator (=) is used. All values are float
so they can accommodate decimal values.
o The next line is for the user input. The score variable
gets the value, which is the score that the user enters. It
is converted into a float.
o The succeeding lines are included in the if block. It
follows the correct syntax, where conditions are logically
declared as well as the statements are specified.
o The comparison operators are used such as the double
equal sign (==).
o Proper indentations are observed throughout the
program.
Output
o This is the output when the entered
score is beyond the HPS: