Duration | Language | Tools | Linked Lecture |
|---|---|---|---|
50 minutes | C++ | Dev-C++ or Code::Blocks (online compiler as backup) | Lecture 8 |
Mapped outcomes: CLO4 (C++ syntax and semantics) and CLO1 (problem solving) at psychomotor level P3 – Guided Response · PLO2, PLO3, PLO5
1. Lab Objectives
By the end of this 50-minute lab, you will be able to:
- Trace which lines of an if / if-else program run for a given input.
- Write if and if-else statements with correct conditions and braces.
- Find the three classic if-bugs: stray semicolon, = instead of ==, missing braces.
- Test both branches and the boundary value of every decision.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Condition | An expression that is true or false | "Is the balance enough?" |
Branch | One of the paths after a decision | Left or right at a road fork |
Block | Statements inside { } | All the steps for one choice |
Boundary value | A test value exactly at the limit | Checking a lift with exactly its maximum load |
Warning | A compiler message about code that is legal but suspicious | A 'wet floor' sign: you can walk, but be careful |
3. Background
An if runs its block only when the condition is true. An if-else runs exactly one of its two blocks. Always use braces, and never put a semicolon after the condition (Lecture 8).
Reminder if (condition) { // true block } else { // false block } |
Turn on warnings today Dev-C++: Tools → Compiler Options → tick 'Add the following commands when calling the compiler' and type -Wall -Wextra. Code::Blocks: Settings → Compiler → tick 'Enable all common compiler warnings [-Wall]'. |
4. Time Plan (50 minutes)
Part | Activity | Time |
|---|---|---|
— | Briefing & turn on warnings | 3 min |
A | Warm-up trace | 4 min |
B | Predict the output | 3 min |
C | Bug hunt (3 bugs) | 5 min |
D | Graded tasks 1–3 | 31 min |
E | Post-lab questions & submission | 4 min |
Part A: Warm-up Trace (4 min)
For each temperature, tick which of the lines A, B, C, D print. Then run the program to check.
lab6_warmup.cpp #include <iostream> using namespace std;
int main() { int temp; cout << "Temperature: "; cin >> temp; if (temp > 40) { cout << "Heatwave alert" << endl; // line A } if (temp >= 20 && temp <= 30) { cout << "Pleasant" << endl; // line B } else { cout << "Not pleasant" << endl; // line C } cout << "Stay safe" << endl; // line D return 0; } |
temp | A (Heatwave) | B (Pleasant) | C (Not pleasant) | D (Stay safe) |
|---|---|---|---|---|
45 | ||||
25 | ||||
10 |
Part B: Predict the Output (3 min)
lab6_predict.cpp #include <iostream> using namespace std;
int main() { int x = 7, y = 10; if (x > y) cout << "P" << endl; cout << "Q" << endl; if (x % 2 == 1) { cout << "R" << endl; } else { cout << "S" << endl; } if (!(y > 5)) cout << "T" << endl; else cout << "U" << endl; return 0; } |
My prediction |
Part C: Bug Hunt (5 min)
Build each program with warnings on. Write the warning or error, the wrong output, and the fix.
bug1.cpp — input 15
#include <iostream> using namespace std;
int main() { int age; cout << "Age: "; cin >> age; if (age >= 18); { cout << "You can get a driving licence" << endl; } return 0; } |
Warning / error, wrong output, fix |
bug2.cpp — input 1111
#include <iostream> using namespace std;
int main() { int pin; cout << "Enter PIN: "; cin >> pin; if (pin = 4321) { cout << "Access granted" << endl; } else { cout << "Wrong PIN" << endl; } return 0; } |
Warning / error, wrong output, fix |
bug3.cpp
#include <iostream> using namespace std;
int main() { int marks; cout << "Marks: "; cin >> marks; if (marks >= 50) cout << "PASS" << endl; cout << "Well done!" << endl; else cout << "FAIL" << endl; return 0; } |
Warning / error, wrong output, fix |
Part D: Graded Lab Tasks (31 min · 10 marks)
Rules for every task Always use braces. Use named constants for fixed numbers. Run every test value listed under the task and write the outputs in your lab book. Save as lab6_task1.cpp, lab6_task2.cpp, lab6_task3.cpp. |
Task 1: Mobile Package (Easy — 3 marks)
Input the mobile balance and the price of a package. If the balance is enough, subscribe: subtract the price and show the remaining balance. Otherwise show how much more the user must recharge. Test: 500/300, 120/300 and the boundary 300/300.
Sample run 1 Mobile balance (Rs.): 500 Package price (Rs.): 300 Package subscribed! Remaining balance: Rs. 200 |
Sample run 2 Mobile balance (Rs.): 120 Package price (Rs.): 300 Not enough balance. Please recharge at least Rs. 180 |
Task 2: ATM Rules (Medium — 3 marks)
An ATM gives cash only if all three rules hold: the amount is a multiple of 500, it is not more than the balance, and it is between 1 and Rs. 25,000 (limit per transaction). Store each rule in a bool variable, then use one if-else. If the withdrawal is refused, print every rule that failed (use separate ifs inside the else).
Sample run 1 Account balance (Rs.): 40000 Amount to withdraw (Rs.): 5000 Please take your cash: Rs. 5000 New balance: Rs. 35000 |
Sample run 2 Account balance (Rs.): 3000 Amount to withdraw (Rs.): 30250 Transaction not allowed. - Amount must be a multiple of 500. - Insufficient balance. - Amount must be between 1 and 25000. |
Also test: 40000 / 5300 (only the multiple-of-500 rule fails) and 40000 / 25000 (boundary — allowed).
Task 3: Weekly Pay with Overtime (Challenge — 4 marks)
A factory worker in Faisalabad is paid per hour. Hours above 40 are overtime, paid at 1.5 × the normal rate. If the gross weekly pay is more than Rs. 50,000, 5% tax is deducted (example rule). Print normal pay, overtime pay, gross pay, tax and net pay with 2 decimals. Use constants for 40, 1.5, 50000 and 0.05.
Sample run 1 Hours worked this week: 48 Rate per hour (Rs.): 1200 Normal pay : Rs. 48000.00 Overtime pay : Rs. 14400.00 Gross pay : Rs. 62400.00 Tax : Rs. 3120.00 Net pay : Rs. 59280.00 |
Sample run 2 Hours worked this week: 38 Rate per hour (Rs.): 900 Normal pay : Rs. 34200.00 Overtime pay : Rs. 0.00 Gross pay : Rs. 34200.00 Tax : Rs. 0.00 Net pay : Rs. 34200.00 |
Also test: exactly 40 hours (no overtime) and a pay of exactly Rs. 50,000 (no tax, because the rule says more than).
Part E: Post-Lab Questions (finish as homework if needed)
Q1. In Part A, why can both 'Heatwave alert' and 'Not pleasant' print for 45?
Answer |
Q2. bug2.cpp printed 'Access granted' for every PIN. Explain why and why this is dangerous.
Answer |
Q3. Which boundary values did you test in Task 3 and why?
Answer |
Marking Rubric (10 marks)
Component | Marks | What earns full marks |
|---|---|---|
Task 1 | 3 | Correct condition, both branches, boundary 300/300 tested. |
Task 2 | 3 | Three named bools; one combined decision; every failed rule reported. |
Task 3 | 4 | Constants; overtime only on hours above 40; independent tax rule; formatted slip; boundaries tested. |
Parts A–C and the post-lab questions are checked in the viva. A program that does not compile gets at most half marks for that task.
Submission
- Show your running programs and test results to the instructor before leaving.
- Copy the final code and the outputs of all test values into your Lab Book (Practical 6 pages).
- Copying code from a classmate gets zero marks for both students.
Next practical (same week): Practical 7 — Decisions II: else-if ladder & switch (Lectures 9 and 10). |