Lecture on Conditional Statements in Pseudocode (IGCSE Paper 2)
Introduction to Conditional Statements
Conditional statements allow a program to make decisions based on certain conditions.
They help control the flow of the program by executing different blocks of code
depending on whether a condition is true or false.
In pseudocode, we use:
IF...THEN...ELSE for simple decisions
IF...ELSE IF...ELSE for multiple conditions
CASE...OF...OTHERWISE for multiple choices
1. Simple IF Statement
Pseudocode Example:
BEGIN
INPUT age
IF age >= 18 THEN
OUTPUT "You are eligible to vote."
ENDIF
END
Equivalent Python Code:
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
2. IF-ELSE Statement
Pseudocode Example:
BEGIN
INPUT number
IF number MOD 2 = 0
THEN
OUTPUT "Even number"
ELSE
OUTPUT "Odd number"
ENDIF
END
Equivalent Python Code:
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even number")
else:
print("Odd number")
3. IF-ELSE IF-ELSE Statement
Pseudocode Example:
BEGIN
INPUT marks
IF marks >= 90 THEN
OUTPUT "Grade: A"
ELSE IF marks >= 80 THEN
OUTPUT "Grade: B"
ELSE IF marks >= 70 THEN
OUTPUT "Grade: C"
ELSE
OUTPUT "Grade: Fail"
ENDIF
END
Equivalent Python Code:
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
else:
print("Grade: Fail")
4. CASE Statement (Switch Equivalent)
Pseudocode Example:
BEGIN
INPUT day
CASE day OF
"Monday": OUTPUT "Start of the week."
"Friday": OUTPUT "Weekend is near!"
"Sunday": OUTPUT "Weekend!"
OTHERWISE OUTPUT "A regular day."
ENDCASE
END
Equivalent Python Code (Using Dictionary):
day = input("Enter the day: ")
response = {
"Monday": "Start of the week.",
"Friday": "Weekend is near!",
"Sunday": "Weekend!"
}
print(response.get(day, "A regular day."))
Activity 7.4:
Re-write a algorithm(program) to check for a mark between 0 and 20, and a pass mark
of 10.
Activity 7.5:
Write a Case Statement to display day of the weak if the Variable day has a whole
number values between 1 and 7 and an error message otherwise.
Another example of Case statements
BEGIN
INPUT month
CASE month OF
"January", "March", "May", "July", "August", "October", "December": OUTPUT "31 days"
"April", "June", "September", "November": OUTPUT "30 days"
"February": OUTPUT "28 or 29 days"
OTHERWISE OUTPUT "Invalid month"
ENDCASE
END
Equivalent Python Code:
python
CopyEdit
month = input("Enter month: ")
response = {
"January": "31 days", "March": "31 days", "May": "31 days",
"July": "31 days", "August": "31 days", "October": "31 days", "December": "31 days",
"April": "30 days", "June": "30 days", "September": "30 days", "November": "30 days",
"February": "28 or 29 days"
}
print(response.get(month, "Invalid month"))
Practice Questions for Student
1. Write pseudocode to check if a number is positive, negative, or zero.
2. Convert the pseudocode below into Python code:
3. BEGIN
4. INPUT temperature
5. IF temperature > 30 THEN
6. OUTPUT "It is hot."
7. ELSE IF temperature < 10 THEN
8. OUTPUT "It is cold."
9. ELSE
10. OUTPUT "It is moderate."
11. ENDIF
12. END
13. Create a program that asks for a user's favorite color and responds based on
predefined choices.
Re-write a algorithm(program) to check for a mark between 0 and 20, and a pass mark
of 10.
Input number : integer
If number >=0 AND number <=20
Then
If number>=10
Then Output “you have passed”
Else
Output “ you have failed
ENDIF
ENDIF
14. Write pseudocode to check if a number is positive, negative, or zero.
Declare number: integer ( 0)
If number>=0
Then Output “ Positive”
ELse If number<0
Then Output “ Negative”
Else
Then “ number is zero”
ENDIF
15. Create a program that asks for a user's favorite color and responds based on
predefined choices.
Declare color: String
if color==”Red”
Then output you are brave
Else color== “ Blue “
Then output you are good in football
ENdIF
Input num1= ( “enter your first number “)
5. Write a Pseudocode program to compare two numbers and print the larger one.
DECLARE Num1 : INTEGER
DECLARE Num2 : INTEGER
DECLARE Bigger : INTEGER
INPUT Num1,Num2
If num1>num2
Then output num1 is greater
Else
Then output num2 is greater
. Write a Pseudocode program to check if a number is divisible by both 3 and 5
Declare num: integer
If (num MOD 3= 0 ) AND (num MOD 5=0)
Then output ( the number is divisible by 3 and 5)
If num==3 and num==5
then output ( GOOD)
IF
Else output (BAD)
ELSE
Ouput ( very excellent ) 10