Professional Documents
Culture Documents
Using If-Else Statments
Using If-Else Statments
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”
Enter PIN
Yes
If PIN = code? Output “The PIN is correct.”
No
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:
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: