SELECTION
[Link] THEN ELSE AND [Link] IF
An IF THEN ELSE structure is a selection control structure that allows a program to make a
decision and execute different statements depending on whether a given condition is true or
false.
OR
An IF THEN ELSE STRUCTURE is a selection control structure that executes one set of statements if a
condition is true and another set of statements if the condition is false.
It is used when a program needs to choose between two alternatives.
Write a pseudocode program that determines whether a number entered is positive or negative.
Answer
START
INPUT Number
IF Number >= 0 THEN
PRINT "Positive Number"
ELSE
PRINT "Negative Number"
ENDIF
STOP
Advantages of IF THEN ELSE Structure
1. It can test complex conditions using relational and logical operators.
2. It can handle ranges of values.
3. It is more flexible than CASE structures.
4. It can be used to make decisions based on calculations.
Disadvantages of IF THEN ELSE Structure
1. Many IF statements can make a program long and difficult to understand.
2. Nested IF statements can become complicated and difficult to debug.
3. It may require more processing time when many conditions are tested.
4. It is less organised than CASE when dealing with many fixed choices.
IF I PUT “IF” ON THE NEXT LINE INSTEAD OF HAVING IT FIXED TO “ELSE IF” IT
CHANGES FROM IF ELSE LADDER TO NESTED IF
NESTED IF IF THEN ELSE
IF Marks >= 50 THEN
IF Salary < 20000 THEN
PRINT "Pass"
Tax ← 0
ELSE ELSE
IF Salary <= 50000 THEN PRINT "Fail"
Tax ← Salary * 0.10 ENDIF
ELSE There is only one decision.
Tax ← Salary * 0.20
ENDIF
ENDIF
NB
Multiple IF ELSE conditions (not necessarily nested):
IF Marks >= 80 THEN
Grade ← "A"
ELSE IF Marks >= 70 THEN
Grade ← "B"
ELSE IF Marks >= 60 THEN
Grade ← "C"
ELSE
Grade ← "D"
ENDIF
This is called an IF-ELSE-IF ladder, not a nested IF, because the IF statements are connected in
sequence rather than placed inside each other.
NB
IF-ELSE-IF Ladder
Usually has one ENDIF because all conditions belong to the same decision chain.
NESTED IF
Has multiple ENDIF
QUESTION 1 (20 Marks) — Hospital Billing System
A hospital calculates a patient's bill using the following rules:
Inputs:
Patient category (Child, Adult, Senior)
Number of days admitted
Insurance status (YES or NO)
Total treatment cost
Rules:
1. If the patient has insurance:
o If the treatment cost is above 100,000:
Insurance covers 80% of the cost.
o Otherwise:
Insurance covers 60% of the cost.
2. If the patient does not have insurance:
o If the patient is a senior:
Give a 20% discount.
o Otherwise:
No discount.
3. If days admitted are more than 14 days, add a service charge of 5,000.
Calculate and display the amount payable.
ANSWER
START
INPUT Category
INPUT Days
INPUT Insurance
INPUT Cost
IF Insurance = "YES" THEN
IF Cost > 100000 THEN
Covered ← Cost * 0.80
ELSE
Covered ← Cost * 0.60
ENDIF
Payable ← Cost - Covered
ELSE
IF Category = "Senior" THEN
Discount ← Cost * 0.20
ELSE
Discount ← 0
ENDIF
Payable ← Cost - Discount
ENDIF
IF Days > 14 THEN
Payable ← Payable + 5000
ENDIF
PRINT Payable
STOP
Question 2 (15 Marks) — University Admission System
A university admits students based on the following conditions:
Input:
Mean grade
Mathematics grade
English grade
Rules:
1. If the mean grade is A or B:
o If Mathematics and English are both B and above, display:
"Admitted to Engineering"
o Otherwise display:
"Admitted to General Programme"
2. If the mean grade is C or below:
o If Mathematics is A, display:
"Special Consideration"
o Otherwise display:
"Not Admitted"
Answer
START
INPUT MeanGrade
INPUT Math
INPUT English
IF MeanGrade = "A" OR MeanGrade = "B" THEN
IF Math >= "B" AND English >= "B" THEN
PRINT "Admitted to Engineering"
ELSE
PRINT "Admitted to General Programme"
ENDIF
ELSE
IF Math = "A" THEN
PRINT "Special Consideration"
ELSE
PRINT "Not Admitted"
ENDIF
ENDIF
STOP
Question 2 (15 Marks) — Bank Loan Approval
A bank approves loans using these rules:Input:
Age
Monthly income
Credit rating
Rules:
1. If age is 18 or above:
o If income is above 50,000:
If credit rating is "Good", approve loan.
Otherwise reject.
o Otherwise reject.
2. If age is below 18:
o Reject loan.
Answer
START
INPUT Age
INPUT Income
INPUT Credit
IF Age >= 18 THEN
IF Income > 50000 THEN
IF Credit = "Good" THEN
PRINT "Loan Approved"
ELSE
PRINT "Loan Rejected"
ENDIF
ELSE
PRINT "Loan Rejected"
ENDIF
ELSE
PRINT "Loan Rejected"
ENDIF
STOP
Question 3 (15 Marks) — Electricity Billing System
An electricity company charges customers according to usage.
Input:
Units consumed
Customer type
Rules:
1. If customer type is Domestic:
o If units are below 100, charge 5 per unit.
o Otherwise charge 8 per unit.
2. If customer type is Business:
o If units are below 500, charge 10 per unit.
o Otherwise charge 15 per unit.
Calculate the total bill.
Answer
START
INPUT Units
INPUT CustomerType
IF CustomerType = "Domestic" THEN
IF Units < 100 THEN
Rate ← 5
ELSE
Rate ← 8
ENDIF
ELSE
IF Units < 500 THEN
Rate ← 10
ELSE
Rate ← 15
ENDIF
ENDIF
Bill ← Units * Rate
PRINT Bill
STOP
Question 5 (15 Marks) — Online Shopping Discount System
A shop gives discounts according to customer status and purchase amount.
Input:
Customer type
Amount purchased
Rules:
1. If customer is a Member:
o If purchase amount is above 20,000, discount is 20%.
o Otherwise discount is 10%.
2. If customer is not a Member:
o If purchase amount is above 20,000, discount is 5%.
o Otherwise no discount.
Calculate final amount payable.
Answer
START
INPUT CustomerType
INPUT Amount
IF CustomerType = "Member" THEN
IF Amount > 20000 THEN
Discount ← Amount * 0.20
ELSE
Discount ← Amount * 0.10
ENDIF
ELSE
IF Amount > 20000 THEN
Discount ← Amount * 0.05
ELSE
Discount ← 0
ENDIF
ENDIF
FinalAmount ← Amount - Discount
PRINT FinalAmount
STOP
Question 6 (15 Marks) — Employee Salary System
A company calculates an employee's net salary using the following rules:
Input:
o Basic salary
o Years of service
o Department code
Rules:
1. If the employee has worked for more than 10 years:
o If salary is above 50,000, give a bonus of 10,000.
o Otherwise, give a bonus of 5,000.
2. If the employee has worked for 10 years or less:
o If salary is above 50,000, give a bonus of 3,000.
o Otherwise, no bonus.
3. Calculate and display the final salary.
Answer
START
INPUT Salary
INPUT Years
IF Years > 10 THEN
IF Salary > 50000 THEN
Bonus ← 10000
ELSE
Bonus ← 5000
ENDIF
ELSE
IF Salary > 50000 THEN
Bonus ← 3000
ELSE
Bonus ← 0
ENDIF
ENDIF
FinalSalary ← Salary + Bonus
PRINT FinalSalary
STOP
[Link] STRUCTURE
A CASE structure is a selection control structure that selects and executes one block of
statements from several alternatives based on the value of a single expression or variable.
State two advantages of using a CASE structure instead of an IF...ELSE structure.
Answer
1. It makes programs easier to read and understand.
2. It is more suitable when selecting from many possible values of the same variable.
Question 1 (6 Marks)
Write a pseudocode program that accepts a student's grade and displays the corresponding
comment.
A – Excellent
B – Very Good
C – Good
D – Fair
E – Poor
Any other grade – Invalid Grade
Answer
START
INPUT Grade
CASE Grade OF
"A":
PRINT "Excellent"
"B":
PRINT "Very Good"
"C":
PRINT "Good"
"D":
PRINT "Fair"
"E":
PRINT "Poor"
OTHERWISE:
PRINT "Invalid Grade"
ENDCASE
STOP
Question 2
Write a pseudocode program that displays a restaurant menu and calculates the total bill.
Code Meal Price (KSh)
1 Chips 150
2 Pilau 250
3 Chicken 400
4 Fish 350
The program should:
Accept the meal code.
Accept the quantity ordered.
Calculate and display the total amount payable.
Display "Invalid Meal Code" if an incorrect code is entered.
Answer
START
INPUT MealCode
INPUT Quantity
CASE MealCode OF
1:
Price ← 150
2:
Price ← 250
3:
Price ← 400
4:
Price ← 350
OTHERWISE:
PRINT "Invalid Meal Code"
STOP
ENDCASE
Total ← Price * Quantity
PRINT "Total Bill = ", Total
STOP
Question 3 (12 Marks)
Write a pseudocode program that allows a user to choose a mathematical operation.
Choice Operation
1 Addition
2 Subtraction
3 Multiplication
4 Division
The program should:
Input two numbers.
Perform the selected operation.
Display the answer.
If division is selected and the second number is zero, display "Cannot Divide by Zero".
Display "Invalid Choice" if necessary.
Answer
START
INPUT Num1
INPUT Num2
INPUT Choice
CASE Choice OF
1:
Answer ← Num1 + Num2
2:
Answer ← Num1 - Num2
3:
Answer ← Num1 * Num2
4:
IF Num2 = 0 THEN
PRINT "Cannot Divide by Zero"
STOP
ELSE
Answer ← Num1 / Num2
ENDIF
OTHERWISE:
PRINT "Invalid Choice"
STOP
ENDCASE
PRINT "Answer = ", Answer
STOP
Question 4 (15 Marks)
Write a pseudocode program that accepts an employee's department code and basic salary.
Code Department Allowance
A Accounts 5,000
H Human Resource 7,000
I ICT 10,000
M Marketing 6,000
The program should:
Calculate gross salary.
Deduct 10% tax.
Display the net salary.
Answer
START
INPUT Department
INPUT BasicSalary
CASE Department OF
"A":
Allowance ← 5000
"H":
Allowance ← 7000
"I":
Allowance ← 10000
"M":
Allowance ← 6000
OTHERWISE:
PRINT "Invalid Department"
STOP
ENDCASE
GrossSalary ← BasicSalary + Allowance
Tax ← GrossSalary * 0.10
NetSalary ← GrossSalary - Tax
PRINT "Gross Salary = ", GrossSalary
PRINT "Tax = ", Tax
PRINT "Net Salary = ", NetSalary
STOP
Question 5 (15 Marks)
A bus company charges fares according to destination.
Code Destination Fare (KSh)
N Nairobi 1,200
M Mombasa 2,000
K Kisumu 1,600
E Eldoret 1,400
The program should:
Accept the destination code.
Accept the number of passengers.
Calculate the total fare.
If more than 10 passengers travel, give a 5% discount.
Display the final amount payable.
START
INPUT Destination
INPUT Passengers
CASE Destination OF
"N":
Fare ← 1200
"M":
Fare ← 2000
"K":
Fare ← 1600
"E":
Fare ← 1400
OTHERWISE:
PRINT "Invalid Destination"
STOP
ENDCASE
Total ← Fare * Passengers
IF Passengers > 10 THEN
Discount ← Total * 0.05
ELSE
Discount ← 0
ENDIF
FinalAmount ← Total - Discount
PRINT "Amount Payable = ", FinalAmount
STOP
State two disadvantages of the CASE structure.
Answer:
It can only test one variable or expression at a time.
It cannot be used to test complex conditions or ranges of values.
ITERATION
1. FOR LOOP
A FOR loop is an iteration control structure that repeats a block of statements a specified
number of times using a loop counter.
State advantages of using a FOR loop.
1. Suitable when the number of repetitions is known.
2. Shorter and easier to read than repeating statements manually.
3. The counter is updated automatically.
4. Reduces programming errors caused by manually updating the counter.
5. Makes programs easier to maintain.
Disadvantages of the FOR Loop
1. Not suitable when the number of repetitions is unknown.
2. Less flexible than a WHILE or REPEAT...UNTIL loop.
3. The counter variable should not normally be changed inside the loop.
4. Not suitable for loops that depend on user input or unpredictable conditions.
Write a pseudocode program that uses a FOR loop to input the marks of 10 students, calculate the total
marks and the average, then display the results.
Answer:
START
Total ← 0
FOR Student ← 1 TO 10
INPUT Mark
Total ← Total + Mark
NEXT Student
Average ← Total / 10
PRINT "Total Marks = ", Total
PRINT "Average = ", Average
STOP
KCSE Exam Tip: Use a FOR loop when the question specifies a fixed number of repetitions, such as 10 students,
12 months, 50 employees, or 100 numbers. If the number of repetitions depends on a condition or user choice, a
WHILE or REPEAT...UNTIL loop is usually more appropriate.
WHY HAVE YOU WRITTEN ":TOTAL=TOTAL+MARK "INSTEAD OF "TOTAL=MARK1+MARK2 "ALL THE WAY TO
MARK10
Great question. The reason is that in a FOR loop, you do not create separate variables like Mark1, Mark2, ...,
Mark10.
Instead, the loop uses the same variable (Mark) repeatedly for each iteration.
What happens inside the loop?
Total ← 0
FOR Student ← 1 TO 10
INPUT Mark
Total ← Total + Mark
NEXT Student
Suppose the user enters these marks:
Iteration Mark Entered Total Calculation Total
1 60 0 + 60 60
2 70 60 + 70 130
3 80 130 + 80 210
4 50 210 + 50 260
... ... ... ...
10 75 Previous Total + 75 Final Total
Notice that Mark changes each time the loop repeats.
Why not write:
Total ← Mark1 + Mark2 + Mark3 + ... + Mark10
Because that defeats the purpose of using a loop.
You would first have to write:
INPUT Mark1
INPUT Mark2
INPUT Mark3
INPUT Mark4
...
INPUT Mark10
Total ← Mark1 + Mark2 + Mark3 + ... + Mark10
This is:
❌ Longer
❌ Repetitive
❌ Hard to maintain
❌ Not making use of the FOR loop
Why Total ← Total + Mark is better
The FOR loop is designed to repeat the same instruction automatically.
Instead of writing 10 input statements and 10 additions, you write one input statement and one addition
statement, and the loop executes them 10 times.
KCSE examiner's expectation
If a question says:
"Input the marks of 10 students using a FOR loop..."
The expected solution is:
Total ← 0
FOR Student ← 1 TO 10
INPUT Mark
Total ← Total + Mark
NEXT Student
Writing Mark1 + Mark2 + ... + Mark10 would still give the correct total, but it does not demonstrate
proper use of the FOR loop, so you could lose marks because the examiner wants you to show that you understand
iteration.
A simple way to remember it:
Without a loop: Mark1, Mark2, ..., Mark10.
With a loop: reuse one variable (Mark) and keep updating the running total with Total ← Total +
Mark.
A school records examination results for 40 students.
For each student:
Input Name.
Input Marks.
Award grades:
Marks Grade
80–100 A
70–79 B
60–69 C
50–59 D
Below 50 E
Display each student's name and grade.
Finally display:
Number of Grade A students.
Number who failed.
Answer
START
GradeA ← 0
Failed ← 0
FOR Student ← 1 TO 40
INPUT Name
INPUT Marks
IF Marks >= 80 THEN
Grade ← "A"
GradeA ← GradeA + 1
ELSE IF Marks >= 70 THEN
Grade ← "B"
ELSE IF Marks >= 60 THEN
Grade ← "C"
ELSE IF Marks >= 50 THEN
Grade ← "D"
ELSE
Grade ← "E"
Failed ← Failed + 1
ENDIF
PRINT Name
PRINT Grade
NEXT Student
PRINT "Grade A =", GradeA
PRINT "Failed =", Failed
STOP
A bank processes transactions for 25 customers.
For every customer:
Input Account Number.
Input Deposit Amount.
If deposit exceeds KSh 100,000, award a bonus of KSh 2,000.
Display each customer's final balance.
Calculate the total amount deposited.
Answer
START
TotalDeposit ← 0
FOR Customer ← 1 TO 25
INPUT AccountNumber
INPUT Deposit
IF Deposit > 100000 THEN
Balance ← Deposit + 2000
ELSE
Balance ← Deposit
ENDIF
PRINT AccountNumber
PRINT Balance
TotalDeposit ← TotalDeposit + Deposit
NEXT Customer
PRINT "Total Deposits =", TotalDeposit
STOP
A school has 50 candidates who sat for the KCSE examination.
Write a pseudocode program that:
1. Inputs each student's:
o Admission Number
o Name
o Marks (0–100)
2. Award grades using the following criteria:
Marks Grade
80–100 A
70–79 B
60–69 C
50–59 D
Below 50 E
3. Display each student's:
o Admission Number
o Name
o Grade
4. At the end of the program display:
o Total marks
o Average marks
o Highest mark
o Lowest mark
o Number of Grade A students
o Number of students who failed (Grade E)
o Pass percentage (Marks ≥ 50)
Answer
START
Total ← 0
GradeA ← 0
Failed ← 0
Passed ← 0
FOR Student ← 1 TO 50
INPUT AdmNo
INPUT Name
INPUT Marks
Total ← Total + Marks
IF Student = 1 THEN
Highest ← Marks
Lowest ← Marks
ELSE
IF Marks > Highest THEN
Highest ← Marks
ENDIF
IF Marks < Lowest THEN
Lowest ← Marks
ENDIF
ENDIF
IF Marks >= 80 THEN
Grade ← "A"
GradeA ← GradeA + 1
Passed ← Passed + 1
ELSE IF Marks >= 70 THEN
Grade ← "B"
Passed ← Passed + 1
ELSE IF Marks >= 60 THEN
Grade ← "C"
Passed ← Passed + 1
ELSE IF Marks >= 50 THEN
Grade ← "D"
Passed ← Passed + 1
ELSE
Grade ← "E"
Failed ← Failed + 1
ENDIF
PRINT AdmNo
PRINT Name
PRINT Grade
NEXT Student
Average ← Total / 50
PassPercentage ← (Passed / 50) * 100
PRINT "Total Marks =", Total
PRINT "Average =", Average
PRINT "Highest =", Highest
PRINT "Lowest =", Lowest
PRINT "Grade A =", GradeA
PRINT "Failed =", Failed
PRINT "Pass Percentage =", PassPercentage,"%"
STOP
2. WHILE LOOP
A WHILE loop is an iteration control structure that repeatedly executes a block of statements as
long as a specified condition remains true.
Difference Between FOR and WHILE Loops
FOR Loop WHILE Loop
Number of repetitions is known. Number of repetitions may be unknown.
Counter is updated automatically. Counter must usually be updated manually.
Best for counting loops. Best for condition-controlled loops.
Has a start and end value. Repeats while a condition remains true.
Advantages
1. Suitable when the number of repetitions is unknown.
2. Can continue until a particular condition is met.
3. More flexible than a FOR loop.
4. Useful for input validation and menu-driven programs.
Disadvantages
1. Can result in an infinite loop if the control variable is not updated.
2. The programmer must manually initialise and update the control variable.
3. Slightly more complex than a FOR loop.
4. Less suitable when the number of iterations is known.
Common Mistake to Avoid
❌ Forgetting to update the loop variable:
WHILE Number <= 10 DO
PRINT Number
ENDWHILE
This creates an infinite loop because Number never changes.
✔ Correct version:
WHILE Number <= 10 DO
PRINT Number
Number ← Number + 1
ENDWHILE
Without updating Number, the condition Number <= 10 remains true forever.
A supermarket records customers' purchases until -1 is entered.
Write a pseudocode program to:
Input each purchase amount.
If the purchase exceeds KSh 10,000, give a 10% discount.
Otherwise give a 5% discount.
Display the amount payable.
Count the number of customers receiving the 10% discount.
Display the total sales after discounts.
Answer
START
Sales ← 0
TenPercent ← 0
INPUT Purchase
WHILE Purchase <> -1 DO
IF Purchase > 10000 THEN
Discount ← Purchase * 0.10
TenPercent ← TenPercent + 1
ELSE
Discount ← Purchase * 0.05
ENDIF
Payable ← Purchase - Discount
PRINT Payable
Sales ← Sales + Payable
INPUT Purchase
ENDWHILE
PRINT "Total Sales =", Sales
PRINT "10% Discounts =", TenPercent
STOP
A company enters employees' salaries until -1 is entered.
The program should:
Calculate tax:
o Salary above KSh 60,000 → 20%
o Otherwise → 10%
Calculate net salary.
Display the net salary.
Count employees earning above KSh 60,000.
Display the total tax collected.
Answer
START
TotalTax ← 0
HighEarners ← 0
INPUT Salary
WHILE Salary <> -1 DO
IF Salary > 60000 THEN
Tax ← Salary * 0.20
HighEarners ← HighEarners + 1
ELSE
Tax ← Salary * 0.10
ENDIF
NetSalary ← Salary - Tax
PRINT NetSalary
TotalTax ← TotalTax + Tax
INPUT Salary
ENDWHILE
PRINT "Total Tax =", TotalTax
PRINT "High Earners =", HighEarners
STOP
A bank accepts deposits until the customer enters Account Number = 0.
For every customer:
Input Account Number.
Input Deposit Amount.
If the deposit is above KSh 100,000, award a bonus of KSh 2,000.
Display the final deposit.
Calculate the total deposits received.
Answer
START
Total ← 0
INPUT Account
WHILE Account <> 0 DO
INPUT Deposit
IF Deposit > 100000 THEN
Deposit ← Deposit + 2000
ENDIF
PRINT Deposit
Total ← Total + Deposit
INPUT Account
ENDWHILE
PRINT Total
STOP
A bank records customer deposits until 0 is entered as the account number.
For each customer:
Input Account Number.
Input Deposit Amount.
If the deposit is at least KSh 50,000, award a bonus of KSh 1,000.
Otherwise no bonus.
Display the final account balance.
Calculate the total amount paid to customers (including bonuses).
Count the number of customers who received bonuses.
Answer
START
Total ← 0
BonusCount ← 0
INPUT Account
WHILE Account <> 0 DO
INPUT Deposit
IF Deposit >= 50000 THEN
Balance ← Deposit + 1000
BonusCount ← BonusCount + 1
ELSE
Balance ← Deposit
ENDIF
PRINT Balance
Total ← Total + Balance
INPUT Account
ENDWHILE
PRINT "Total Paid =", Total
PRINT "Bonuses =", BonusCount
STOP
3. REPEAT UNTIL
A REPEAT...UNTIL loop is an iteration control structure that repeatedly executes a block
of statements until a specified condition becomes true.
Advantages
1. Executes the loop body at least once.
2. Suitable when user input is required before testing a condition.
3. Useful for menu-driven programs and input validation.
4. Easy to understand for condition-controlled repetition.
Disadvantages
1. Can result in an infinite loop if the terminating condition is never met.
2. Less suitable when the condition should be checked before any execution.
3. Requires careful design of the terminating condition.
Difference Between REPEAT...UNTIL and WHILE
REPEAT...UNTIL WHILE
Exit-controlled loop. Entry-controlled loop.
Condition tested after execution. Condition tested before execution.
Executes at least once. May execute zero times.
Stops when the condition becomes TRUE. Continues while the condition is TRUE.
Difference Between REPEAT...UNTIL and FOR
REPEAT...UNTIL FOR
Number of repetitions is unknown. Number of repetitions is known.
Controlled by a condition. Controlled by a counter.
Executes at least once. May not execute if the range is invalid.
Write a pseudocode program using a REPEAT...UNTIL loop to display
numbers from 1 to 20.
START
Number ← 1
REPEAT
PRINT Number
Number ← Number + 1
UNTIL Number > 20
STOP
Write a pseudocode program that repeatedly accepts students' marks until
the user enters -1. The program should calculate and display the total marks
entered.
START
Total ← 0
REPEAT
INPUT Mark
IF Mark <> -1 THEN
Total ← Total + Mark
ENDIF
UNTIL Mark = -1
PRINT "Total Marks =", Total
STOP
FINALE
QUESTION 1 (15 Marks)
Selection Structures Only (Nested IF + CASE)
A water company charges customers according to customer type and water units consumed.
Input:
Customer Type
o D = Domestic
o B = Business
Units Consumed
Rules:
If customer type is D:
o Units ≤ 50 → KSh 30 per unit
o Units > 50 → KSh 45 per unit
If customer type is B:
o Units ≤ 100 → KSh 60 per unit
o Units > 100 → KSh 80 per unit
Using CASE and Nested IF, write a pseudocode program to calculate and display the customer's bill.
Answer
START
INPUT CustomerType
INPUT Units
CASE CustomerType OF
"D":
IF Units <= 50 THEN
Rate ← 30
ELSE
Rate ← 45
ENDIF
"B":
IF Units <= 100 THEN
Rate ← 60
ELSE
Rate ← 80
ENDIF
ENDCASE
Bill ← Units * Rate
PRINT Bill
STOP
QUESTION 2 (15 Marks)
Selection Structures Only (CASE + IF)
A bank customer enters a transaction type.
The options are:
D = Deposit
W = Withdrawal
B = Balance Enquiry
Rules:
Deposit
o Amount must be greater than 0.
Withdrawal
o Amount must not exceed the account balance.
Balance enquiry
o Display account balance.
Using CASE and IF, write a pseudocode program.
Answer
START
INPUT Choice
CASE Choice OF
"D":
INPUT Amount
IF Amount > 0 THEN
PRINT "Deposit Successful"
ELSE
PRINT "Invalid Amount"
ENDIF
"W":
INPUT Balance
INPUT Amount
IF Amount <= Balance THEN
PRINT "Withdrawal Successful"
ELSE
PRINT "Insufficient Funds"
ENDIF
"B":
INPUT Balance
PRINT Balance
ENDCASE
STOP
QUESTION 3 (15 Marks)
Iteration Structures Only (FOR + WHILE)
A teacher has 5 classes.
For each class:
Enter students' marks until -1 is entered.
Calculate the total marks for the class.
Display the class total.
Finally display the grand total for all classes.
Answer
START
GrandTotal ← 0
FOR Class ← 1 TO 5
Total ← 0
INPUT Mark
WHILE Mark <> -1 DO
Total ← Total + Mark
INPUT Mark
ENDWHILE
PRINT Total
GrandTotal ← GrandTotal + Total
NEXT Class
PRINT GrandTotal
STOP
QUESTION 4 (15 Marks)
Iteration Structures Only (FOR + REPEAT...UNTIL)
A supermarket has 10 cashiers.
For each cashier:
Enter customer purchases.
Stop entering purchases when -1 is entered.
Display the cashier's total sales.
Finally display the supermarket's total sales.
Answer
START
SuperTotal ← 0
FOR Cashier ← 1 TO 10
Total ← 0
REPEAT
INPUT Purchase
IF Purchase <> -1 THEN
Total ← Total + Purchase
ENDIF
UNTIL Purchase = -1
PRINT Total
SuperTotal ← SuperTotal + Total
NEXT Cashier
PRINT SuperTotal
STOP
QUESTION 5 (15 Marks)
Everything Combined (CASE + Nested IF + FOR + WHILE) ⭐⭐⭐⭐⭐
A school has 3 streams.
For each stream:
Enter students until Admission Number = 0.
For every student:
Input:
Admission Number
Marks
Subject Code
o M = Mathematics
o C = Computer Studies
Rules:
Mathematics
Marks ≥ 50 → PASS
Otherwise → FAIL
Computer Studies
Marks ≥ 40 → PASS
Otherwise → FAIL
For every student:
Display Admission Number.
Display PASS or FAIL.
Finally display:
Total number of students processed.
Number of students who passed.
Number of students who failed.
Answer
START
Passed ← 0
Failed ← 0
Students ← 0
FOR Stream ← 1 TO 3
INPUT AdmNo
WHILE AdmNo <> 0 DO
INPUT Subject
INPUT Marks
Students ← Students + 1
CASE Subject OF
"M":
IF Marks >= 50 THEN
PRINT AdmNo
PRINT "PASS"
Passed ← Passed + 1
ELSE
PRINT AdmNo
PRINT "FAIL"
Failed ← Failed + 1
ENDIF
"C":
IF Marks >= 40 THEN
PRINT AdmNo
PRINT "PASS"
Passed ← Passed + 1
ELSE
PRINT AdmNo
PRINT "FAIL"
Failed ← Failed + 1
ENDIF
ENDCASE
INPUT AdmNo
ENDWHILE
NEXT Stream
PRINT "Students =", Students
PRINT "Passed =", Passed
PRINT "Failed =", Failed
STOP