PROBLEM SOLVING
CONDITIONAL CONTROL
CP1013
INTRODUCTION TO COMPUTING
Week 03
Today’s Content
• Relational Operators
• Logical Operators
• Decision Structure
• If/else
• if/if/if
• If/elseif
• Case Studies for Practice
Relational Operators
• Relational operators (compare two values → Boolean)
1. == equal
Small Drill -> Consider a = 3 and b = 5, then what
2. != not equal
will the given expression print?
3. > greater
• a>b
4. < less
• a<b
5. >= greater-or-equal • a!=b
6. <= less-or-equal • a==b
• a>=b
• a<=b
Logical Operators
• Relational operators (compare two values → Boolean)
AND (&&) both If a is between 10 - 15
true
(a>10) && (a<15)
OR (||) at least
one true
(a>10) || (a<15)
NOT (!) flips
truth ! ((a>10) || (a<15))
Decision Structure
• A decision structure (which is also known as a selection structure) allows a program to perform
actions only under certain conditions.
• We use the ‘if’ keyword to represent a decision
• In a Flowchart, we use a diamond shape for a decision
Example – Find Greatest
Read two integers and print the greatest
number.
Example – Voting Rights
If the age of a person is greater than or equal
to 18. Based on this, it either takes the false
branch and displays "Sorry, not yet" or takes
the true branch and displays "Go vote!".
Decision Structure (continue)
if … if … if … (independent checks) Nested if-else (tiered decisions)
Each if evaluated separately; multiple can run. Useful for ranges/tiers; only one path.
if likes > 500 if engagement >= 12
output "Nice likes!“ label ← "Viral“
if comments > 80 else if engagement >= 6
output "Chatty post! label ← "Strong"
"if saves > 50 else
output "Saved a lot!“ label ← "OK”
Use when: you want multiple messages/actions to trigger
if … else (either/or) - Exactly one branch runs.
if engagement >= 6
output "Strong“
else
output "Keep experimenting“
Tip: When tiers get long, prefer an else-if ladder for readability.
Example –
Find Greatest
Read three integers and print
the greatest number.
Example – Sort
2 integers in
Ascending
Order
Read two integers and sort them
in the right sequence, and print
the updated answer
A diagram of a company
AI-generated content may be incorrect.
Example – Sort
3 integers in
Ascending
Order
Read three integers and sort
them in the right sequence, and
print the updated answer
Example – Sort 4 integers in Ascending
Order (Try yourself)
Read four integers and sort them in the right sequence, and print the updated
answer
Home Task
Read five integers and sort them in the right sequence, and print the updated
answer
Let’s Practice
Example A: Campus Café Checkout
The campus café wants every receipt to be calculated the same way, no matter who
is at the counter. A customer presents their order with a subtotal. If the subtotal is
negative or zero, the system should immediately stop and display “Invalid subtotal”.
Otherwise, the system should apply a membership discount based on the
customer’s membership level—None (0%), Silver (5%), or Gold (10%)—and then
apply sales tax of 8% to the discounted amount. The final result is the final payable
amount
Step 1: I-P-O-C-E
• Inputs: subtotal (double), membership (String)
• Process: discount by membership → then apply 8% tax
• Outputs: finalAmount (double) or "Invalid subtotal"
• Constraints: subtotal > 0; membership is one of the three
• Edge cases: subtotal very small (e.g., 0.01), unknown membership
Step 2: Decision Table
membership discountRate
None 0.00
Silver 0.05
Gold 0.10
Other 0.00 (or reject—your policy)
Step 3: Sample Tests
subtotal membership expected idea
1000.00 Silver 5% off → tax
1000.00 Gold 10% off → tax
500.00 None no discount
0.00 Gold invalid
Step 4: Algorithm
• Step 1: Input subtotal, membership.
• Step 2:If subtotal <= 0 then output "Invalid subtotal" and stop.
• Step 3: Set discountRate ← 0.
• Step 4: If membership = "Silver" then discountRate ← 0.05.
• Step 5: Else if membership = "Gold" then discountRate ← 0.10.
• Step 6: Set discounted ← subtotal × (1 − discountRate).
• Step 7: Set finalAmount ← discounted × 1.08.
• Step 8: Output finalAmount.
• Step 9: End.
Step 4: Pseudocode
INPUT subtotal, membership
IF subtotal <= 0 THEN
PRINT "Invalid subtotal"
STOP
discountRate ← 0
IF membership = "Silver" THEN
discountRate ← 0.05
ELSE IF membership = "Gold" THEN
discountRate ← 0.10
discounted ← subtotal * (1 - discountRate)
finalAmount ← discounted * 1.08
PRINT finalAmount
Step 5:
Flowchart
Example B: Phone Battery Coach
You’re in a 90-minute lecture, and half the class is secretly watching match highlights. Your
phone is dropping fast, so you’re asked to design a tiny Battery Coach that makes one
instant decision and shows a mode on screen. The app asks three things: your battery
percentage, whether the phone is plugged in to charge, and whether a video is playing. If
the battery is 10% or less and you’re not plugged in, it must switch to “Ultra Save.”
Otherwise, if the battery is 25% or less, or you’re watching a video while not plugged in, it
should choose “Save.” In every other situation, it should display “Normal.” Your job is to
turn this story into variables and a clean cascade of conditions that outputs exactly one of
those three labels.
Class Activity
Based on the given case study, perform the following:
1. Identify IPOCE
2. Make a Decision Table
3. Design Pseudocode
4. Draw a Flowchart
Solution (Phone Battery Coach)
1 - IPOCE:
I — battery (integer %, 0–100) · plugged (Boolean) · isVideoPlaying (Boolean)
P — If battery ≤ 10 AND NOT plugged → "Ultra Save"
Else if battery ≤ 25 OR (isVideoPlaying AND NOT plugged) → "Save"
Else → "Normal"
O — mode { "Ultra Save", "Save", "Normal" }
C — No loops; single decision cascade; inputs assumed valid; evaluate rule #1 before rule #2.
E — Exactly battery=10 (Ultra if not plugged) · exactly battery=25 (Save) · video playing while plugged (should not
force Save unless battery ≤ 25).
Solution (Phone Battery Coach)
2 – Decision Table:
Priority Condition set (read top→down; first match wins) Mode
1 battery ≤ 10 AND plugged = false Ultra Save
2 battery ≤ 25 Save
3 isVideoPlaying = true AND plugged = false Save
4 (otherwise) Normal
Solution (Phone Battery Coach)
3 - Pseudocode:
INPUT battery, plugged, isVideoPlaying
IF (battery ≤ 10) AND (NOT plugged) THEN
mode ← "Ultra Save"
ELSE IF (battery ≤ 25) OR (isVideoPlaying AND (NOT plugged)) THEN
mode ← "Save"
ELSE
mode ← "Normal"
END IF
OUTPUT mode
Solution (Phone
Battery Coach)
4 - Flowchart:
Answer the Questions:
Practice Q1: How many outputs will be displayed on
screen?
Output Q2: What outputs will be printed?
Prediction
Q3: What if we turn the condition operator
to < what will happen?
Example C: Electricity Bill (Try Yourself)
The Lahore Electric Supply Company (LESCO) wants to automate the calculation of monthly
electricity bills for domestic consumers.
Currently, the billing officer manually computes the total amount based on the number of
electricity units consumed by a customer and the applicable per-unit charges.
To simplify the process and eliminate human error, LESCO plans to develop a simple console-
based program that calculates the total payable bill for each customer based on their
consumption units and fixed service charges.
According to the company’s tariff policy, the per-unit rates depend on the consumption slab.
The system must allow the user to enter the Customer ID, Customer Name, and the number
of units consumed in the current month.
The program will determine the unit rate based on the defined slab, compute the bill amount,
add a fixed meter rent, and finally display the total payable amount along with the
customer’s information.
Example C: Electricity Bill (Try Yourself)
Tariff Slab Structure
Units Consumed Rate per Unit (Rs.)
1 – 100 10
101 – 200 15
201 – 300 20
301 – 500 25
Above 500 30
Fixed Meter Rent: Rs. 150 (for all customers)
Case Study: Student Management System
Assign Grades
Based on the assigned marks, assign grades (A, A-, B+, B, B-, C+, C, C-, D+, D, F)
Assign GPA Score Points based on the grades assigned
Extend the grading system and assign GPA Points
Calculate GPA
Calculate grade points, then calculate overall GPA