Nexus Learn

Practical 7: Decisions II: else-if Ladder & switch

Lab Practical CS-307 Programming Fundamentals 6 min read Updated 8 Nov 2026
On this page
  1. 1. Lab Objectives
  2. 2. Word Bank
  3. 3. Background
  4. 4. Time Plan (50 minutes)
  5. Part A: Warm-up Trace (3 min)
  6. Part B: Predict the Output (4 min)
  7. Part C: Bug Hunt (5 min)
  8. Part D: Graded Lab Tasks (32 min · 10 marks)
  9. Part E: Post-Lab Questions (finish as homework if needed)
  10. Marking Rubric (10 marks)
  11. Submission

Duration

Language

Tools

Linked Lecture

50 minutes

C++

Dev-C++ or Code::Blocks (online compiler as backup)

Lectures 9 and 10

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 an else-if ladder and a switch for different inputs.
  • Write an else-if ladder in the correct order, with an input check first.
  • Write a menu program with switch, case, break and default.
  • Recognise wrong ladder order, fall-through and switch compile errors.

2. Word Bank

Word

Simple meaning

Real-world example

else-if ladder

A chain of conditions; the first true one wins

Sorting eggs into trays by size

Slab

A range with its own rate

Electricity: first 100 units at one rate

Progressive bill

Each slab's units are charged at that slab's rate

Income tax brackets

switch / case

Jump to the label that matches a value

A lift button for each floor

Fall-through

Running into the next case without break

Lift doors opening at every floor after yours

Menu-driven program

The user picks an option by number

An ATM screen

3. Background

Ladder: one value, many ranges — check conditions top to bottom, first true wins; put validity checks first. switch: one int/char, many exact values — end each case with break; default catches the rest.

4. Time Plan (50 minutes)

Part

Activity

Time

—

Briefing

2 min

A

Warm-up trace of a ladder

3 min

B

Predict switch output

4 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 (3 min)

lab7_warmup.cpp — wind speed warnings (example limits)

#include <iostream>

using namespace std;

int main() {

int speed;

cout << "Wind speed (km/h): ";

cin >> speed;

if (speed >= 90) {

cout << "Storm warning" << endl;

} else if (speed >= 60) {

cout << "Strong wind" << endl;

} else if (speed >= 30) {

cout << "Breezy" << endl;

} else {

cout << "Calm" << endl;

}

return 0;

}

speed

95

60

45

12

Output

Part B: Predict the Output (4 min)

lab7_predict.cpp

#include <iostream>

using namespace std;

int main() {

int code = 2;

switch (code) {

case 1: cout << "Lahore" << endl;

case 2: cout << "Faisalabad" << endl;

case 3: cout << "Multan" << endl; break;

case 4: cout << "Karachi" << endl;

default: cout << "Unknown" << endl;

}

char g = 'B';

switch (g) {

case 'A': cout << "Excellent" << endl; break;

case 'B':

case 'C': cout << "Good" << endl; break;

default: cout << "Try again" << endl;

}

return 0;

}

My prediction

Part C: Bug Hunt (5 min)

bug1.cpp — wrong order

#include <iostream>

using namespace std;

int main() {

int units = 80;

double rate;

if (units <= 300) {

rate = 20;

} else if (units <= 200) {

rate = 15;

} else if (units <= 100) {

rate = 10;

} else {

rate = 25;

}

cout << "Rate: Rs. " << rate << endl;

return 0;

}

Error / wrong output and fix

bug2.cpp — case punctuation

#include <iostream>

using namespace std;

int main() {

int choice = 1;

switch (choice) {

case 1; cout << "Balance" << endl; break;

case 2; cout << "Transfer" << endl; break;

}

return 0;

}

Error / wrong output and fix

bug3.cpp — switch on a double

#include <iostream>

using namespace std;

int main() {

double gpa = 3.5;

switch (gpa) {

case 4.0: cout << "Gold medal" << endl; break;

default: cout << "Keep working" << endl;

}

return 0;

}

Error / wrong output and fix

Part D: Graded Lab Tasks (32 min · 10 marks)

Rules for every task

Put the invalid input check first.

Use constants for rates.

Test every value listed and record the outputs.

Save as lab7_task1.cpp, lab7_task2.cpp, lab7_task3.cpp.

Task 1: Result Grade with Remarks (Easy — 3 marks)

Input marks. Marks below 0 or above 100 are invalid. Otherwise print the grade and a remark using the example scheme: A 85+ Excellent, B 70–84 Very good, C 60–69 Good, D 50–59 Pass, F below 50 Fail. Test: 92, 85, 77, 60, 50, 49, 101, -5.

Sample run

Enter marks (0-100): 77

Grade B - Very good

marks

92

85

77

60

50

49

101

-5

Output

Task 2: Progressive Electricity Bill (Medium — 3 marks)

Example rates: the first 100 units cost Rs. 10 each, the next 100 units Rs. 15 each, and every unit above 200 Rs. 20 each. Negative units are invalid. This is different from Lecture 9: here each slab is charged at its own rate.

units

Working

Bill

80

80 × 10

800

150

100 × 10 + 50 × 15

1750

260

100 × 10 + 100 × 15 + 60 × 20

3700

Sample run

Units used: 150

Bill: Rs. 1750

Also test: 100, 200 (boundaries → 1000 and 2500) and -4 (invalid).

Task 3: Unit Converter Menu (Challenge — 4 marks)

Show this menu, input the choice and a value, and use a switch to convert. Distance and weight cannot be negative (check inside those cases). Any other choice prints "Invalid choice". Use constants: 1 km = 0.621371 miles; 1 kg = 2.20462 pounds; °F = °C × 9 / 5 + 32; Rs. 280 = $1 (example rate). Print 2 decimals.

Sample run

1. Kilometres to miles

2. Kilograms to pounds

3. Celsius to Fahrenheit

4. Rupees to US dollars

Your choice: 1

Value: 180

180.00 km = 111.85 miles

Choice / value

1 / 180

3 / -10

2 / -3

4 / 5600

7 / 3

Output

Part E: Post-Lab Questions (finish as homework if needed)

Q1. Why must the invalid-marks check come first in Task 1?

Answer

Q2. Explain the difference between the flat slab bill (Lecture 9) and the progressive bill (Task 2) for 150 units.

Answer

Q3. In Task 3, what happens if you forget break after case 1 and the user chooses 1?

Answer

Marking Rubric (10 marks)

Component

Marks

What earns full marks

Task 1

3

Invalid check first; ladder in correct order; eight tests recorded.

Task 2

3

Invalid check; correct progressive formulas; boundaries 100 and 200 tested.

Task 3

4

Menu; switch with break in every case and default; negative checks; constants; 2 decimals.

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 outputs into your Lab Book (Practical 7 pages).
  • Copying code from a classmate gets zero marks for both students.

Next practical: Practical 8 — Loops I: while & do-while (Week 5).

← All CS-307 material