Duration | Language | Tools | Linked Lecture |
|---|---|---|---|
50 minutes | C++ | Dev-C++ or Code::Blocks (online compiler as backup) | Lectures 11 and 12 |
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 while and do-while loops with a trace table.
- Validate input with a do-while loop.
- Write sentinel-controlled loops that stop on a special value.
- Find infinite loops, wrong validation conditions and a missing semicolon.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Iteration | One round of a loop | One lap of the running track |
Validation | Checking input is acceptable | A form that refuses a 12-digit CNIC |
Sentinel | A special value that means stop | A cashier's 'that's all' |
Infinite loop | A loop that never ends | A fan with a stuck switch |
Accumulator | A variable that collects a total | The running total on a shop bill |
3. Background
Reminder while (condition) { // checks first: 0 or more runs … }
do { // runs first: 1 or more runs … } while (condition); // ; needed |
Stopping a runaway program If your program never stops, press Ctrl + C in the console window (or close it). Then fix the loop. |
4. Time Plan (50 minutes)
Part | Activity | Time |
|---|---|---|
— | Briefing | 2 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 | 32 min |
E | Post-lab questions & submission | 4 min |
Part A: Warm-up Trace (4 min)
lab8_warmup.cpp #include <iostream> using namespace std;
int main() { int x = 100, steps = 0; while (x > 1) { x = x / 3; steps++; cout << "x = " << x << endl; } cout << "Steps: " << steps << endl; return 0; } |
Round | x before | x after x / 3 | steps |
|---|---|---|---|
1 | 100 | ||
2 | |||
3 | |||
4 |
Part B: Predict the Output (3 min)
lab8_predict.cpp #include <iostream> using namespace std;
int main() { int a = 5; while (a < 3) { cout << "A" << endl; a++; } do { cout << "B" << endl; a++; } while (a < 3); int b = 1; do { cout << b << " "; b = b * 2; } while (b < 20); cout << endl << "a = " << a << ", b = " << b << endl; return 0; } |
My prediction |
Part C: Bug Hunt (5 min)
bug1.cpp — never stops
#include <iostream> using namespace std;
int main() { int count = 1; while (count <= 3) { cout << "Bismillah" << endl; } return 0; } |
Problem and fix |
bug2.cpp — input 150
#include <iostream> using namespace std;
int main() { int age; do { cout << "Enter age (1-120): "; cin >> age; } while (age < 1 && age > 120); cout << "Age accepted: " << age << endl; return 0; } |
Problem and fix |
bug3.cpp
#include <iostream> using namespace std;
int main() { int price, total = 0; cout << "Enter prices (0 to stop): "; do { cin >> price; total = total + price; } while (price != 0) cout << "Total: " << total << endl; return 0; } |
Problem and fix |
Part D: Graded Lab Tasks (32 min · 10 marks)
Rules Validate input with do-while where asked. Use a trace table if a loop gives a strange answer. Save as lab8_task1.cpp, lab8_task2.cpp, lab8_task3.cpp. |
Task 1: Digit Tools (Easy — 3 marks)
Ask for a positive whole number; keep asking (do-while) until it is greater than 0. Then, with one while loop, find the number of digits, the sum of the digits and the reversed number.
Sample run Enter a positive whole number: -5 Not positive. Try again. Enter a positive whole number: 0 Not positive. Try again. Enter a positive whole number: 4072 Number : 4072 Digits : 4 Sum : 13 Reversed : 2704 |
Task 2: Shop Bill with a Sentinel (Medium — 3 marks)
Read item prices until 0 is entered. A negative price is refused with a message and not added. Print the number of items, the total, a 5% discount if the total is more than Rs. 5,000 (example rule) and the amount to pay, with 2 decimals.
Sample run 1 Enter item prices (0 to finish): 1200 -50 2500 1800 0 Price cannot be negative - ignored. Items : 3 Total : Rs. 5500.00 Discount : Rs. 275.00 To pay : Rs. 5225.00 |
Also test: 450 300 0 (no discount → Rs. 750.00) and 0 straight away (0 items).
Task 3: Number Guessing Game (Challenge — 4 marks)
The secret number is 37 (a constant). The player has at most 7 tries. After each wrong guess print "Too high!" or "Too low!". Stop when the guess is right or the tries are used up, then print the result.
Sample run 1 Guess my number (1-100). You have 7 tries. Guess: 50 Too high! Guess: 25 Too low! Guess: 40 Too high! Guess: 37 Correct! You found it in 4 tries. |
Sample run 2 (guesses 1 to 7) … seven wrong guesses … Out of tries. The number was 37. |
Part E: Post-Lab Questions (finish as homework if needed)
Q1. Why is do-while a better choice than while for Task 1's input check?
Answer |
Q2. In Task 2, why is the second cin placed at the end of the loop body?
Answer |
Q3. Task 1 reverses 4070 as 704, not 0704. Why?
Answer |
Marking Rubric (10 marks)
Component | Marks | What earns full marks |
|---|---|---|
Task 1 | 3 | Validation loop; one while loop for digits, sum and reverse. |
Task 2 | 3 | Correct sentinel pattern; negatives refused; discount and formatting. |
Task 3 | 4 | Two-part stopping condition; hints; correct final message; constants. |
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 to the instructor before leaving.
- Copy the final code and outputs into your Lab Book (Practical 8 pages).
- Copying code from a classmate gets zero marks for both students.
Next practical (same week): Practical 9 — Loops II: for loops, counters & sums (Lecture 13). |