You are on page 1of 12

USING IF-ELSE

STATEMENTS
What is an If-Else
Statement?
An if-else statement is a type of conditional
statement in programming that is used to execute
a block of code based on a certain condition. It
consists of two parts: the "if" condition and the
"else" condition. The "if" condition specifies the
criteria that must be met for the code to be
executed, while the "else" condition specifies what
should happen if the "if" condition is not met.
If-Else Conditional Structure:

True
Condition Statement/s

False

else Statement/s
In Python, its syntax is:

If condition:
statement/s
else:
statements/s
PROGRAM EXAMPLE

Pseudocode:
1. Assign the PIN to a variable
2. Display the instruction to the user to enter his or her PIN.
3. Accept the entered PIN.
4. Evaluate if the entered PIN is the same as the stored PIN.
Display a message if the PIN is correct.
5. Display another message if the PIN is incorrect.
PROGRAM EXAMPLE
Flowchart:
Start

code = “1234”

Output “Please enter the PIN.”

Enter PIN

Yes
If PIN = code? Output “The PIN is correct.”

No

Output “The PIN is incorrect.”

End
Program:
#PIN
#Display a message when the PIN is the same as the stored PIN.
#Else, it displays another message.

code = “1234”
PIN = input(“Please enter the PIN:”)
if PIN == code:
print(“The PIN is correct.”)
else:
print(“The PIN is incorrect.”)
Output of program 1:

Please enter the PIN:5678


The PIN is incorrect
Program:
#Score Remark
#Display a message “You passed the quiz!”, if the score is passing.

passingScore = 7.5
score = float(input(“Please enter your score: “))
if score >= passingScore:
print(“You passed the quiz!”)
else:
print(“Oops. Do better next time.”)
Output of program 2:

Please enter your score: 5


Oops. Do better next time.
Thank
you!

You might also like