0% found this document useful (0 votes)
4 views

Lesson-4.-USING-IF-ELIF-ELSE-STATEMENTS

The document explains the use of If-Elif-Else statements in Python for performing conditional programming. It provides syntax examples and pseudocode for programs that evaluate user input, such as determining quiz scores and identifying if a number is positive, zero, or negative. The document includes sample outputs for various user inputs to illustrate the functionality of the conditional statements.

Uploaded by

Yahni Belandres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lesson-4.-USING-IF-ELIF-ELSE-STATEMENTS

The document explains the use of If-Elif-Else statements in Python for performing conditional programming. It provides syntax examples and pseudocode for programs that evaluate user input, such as determining quiz scores and identifying if a number is positive, zero, or negative. The document includes sample outputs for various user inputs to illustrate the functionality of the conditional statements.

Uploaded by

Yahni Belandres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

ICT 10

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

Yes Output “You


Output If score >=
hps? passed the
“Please enter
quiz!”
your score:”
No

Enter score Output “Oops.


Do better next
time.”
Yes
If score > Output “Your
hps? score is out of
range.” End
No
Program:
#Score Remark
#Display the remarks “Score is out of range.”, “You got a perfect
score!”,
#”You passed the quiz!”, and “Oops, Do better next time.”

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:

Please enter your score: 12


Score is out of range
o This is the output when the
entered score is passing:

Please enter your score: 9


You passed the quiz!
o This is the output when the
entered score is failed:

Please enter your score: 5


Oops. Do better next time.
Example 2: Positive, Zero
or Negative
Let us create a program
that determines if a number
is positive, zero or negative.
Program:
#Positive Zero Negative
#Identify if a number is positive, zero or negative.

number = int(input(“Please a number: “))


if number > 0:
print(“The number”, number, “is positive number.”)
elif number == 0:
print(“The number is zero.”)
elif number < 0 :
print(“The number”, number, ‘is a negative
number.”)
Output
o This is the output when we entered
number is a positive number.

Please enter a number: 18


The number 18 is a positive number.
o This is the output when the
entered number is zero.

Please enter a number: 0


The number is zero.
o This is the output when the
entered number is a negative
number.
Please enter a number: -1
The number -1 is a negative number.

You might also like