Slide 2
Quick Recap of Lecture 9
5 minutes · homework check
Homework 2
Remember
- if (gb <= 2) price = 300;
- else if (gb <= 5) price = 600;
- else if (gb <= 10) price = 1000;
- else price = 1500;
- Nested if: inner checked only if outer is true
- Ladder: the FIRST true condition wins
- >= ladder: biggest limit first
- else belongs to the NEAREST if — use braces
- Today's question
- A menu has options 1, 2, 3, 4. Is there something neater than a long ladder of choice == 1, choice == 2 …?
Recap
Programming Fundamentals · Lecture 10
Slide 3
Today's Plan
50 minutes · 8 parts
Recap & homework check
5 min
Learning outcomes & menus everywhere
3 min
The switch statement
12 min
break, fall-through & the limits of switch
10 min
The conditional operator ?:
6 min
Class activity: phone-banking menu
8 min
Mistakes & recap quiz
5 min
Homework & next lecture
1 min
Programming Fundamentals · Lecture 10
Slide 4
Learning Outcomes (SLOs)
By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)
Write a switch statement with case, break and default
SLO 10.1 → CLO4 · C3
Explain fall-through and decide when switch is better than an else-if ladder
SLO 10.2 → CLO4 · C2
SLO 10.3 → CLO4 · C3
Use the conditional operator ?: for simple two-way choices
CLO4 Describe the syntax and semantics of the C++ programming language.
Programming Fundamentals · Lecture 10
Slide 5
Menus Are Everywhere
Choosing one action from a list of exact values
Bank helpline
ATM
Press 1 for Urdu, 2 for English, 0 for an agent
1 Withdraw · 2 Balance · 3 Mini statement · 4 Exit
Calculator
Week
+ − × ÷ — one action per operator
1 = Monday … 7 = Sunday
Key word: EXACT. switch compares one value with exact constants (1, 2, '+'). Ranges such as marks >= 50 still need if.
Module 1 · The switch Statement
Programming Fundamentals · Lecture 10
Slide 6
The switch Statement
Jump to the case that matches the value
Syntax
- switch (expression) {
- case value1:
- // statements
- break;
- case value2:
- // statements
- break;
- default:
- // no case matched
- }
Real-world: a lift — press 5 and it goes straight to floor 5, without stopping to ask at every floor.
Module 1 · The switch Statement
Programming Fundamentals · Lecture 10
Slide 7
Example: Day Name from a Number
One case for each exact value
day.cpp (decision part)
Run 1
- switch (day) {
- case 1: cout << "Monday" << endl; break;
- case 2: cout << "Tuesday" << endl; break;
- case 3: cout << "Wednesday" << endl; break;
- case 4: cout << "Thursday" << endl; break;
- case 5: cout << "Friday" << endl; break;
- case 6: cout << "Saturday" << endl; break;
- case 7: cout << "Sunday" << endl; break;
- default: cout << "Invalid day" << endl;
- }
- Enter day number (1-7): 5
- Friday
Run 2
- Enter day number (1-7): 7
- Sunday
Run 3
- Enter day number (1-7): 9
- Invalid day
Module 1 · The switch Statement
Programming Fundamentals · Lecture 10
Slide 8
How switch Runs
Find the matching label → run from there → stop at break
No match + default → default runs.
No match + no default → nothing runs.
Module 1 · The switch Statement
Programming Fundamentals · Lecture 10
Slide 9
Example: A Simple Calculator
switch on a char, with an if inside one case
calc.cpp (main part)
- cin >> a >> op >> b;
- switch (op) {
- case '+': cout << "Result: " << a + b << endl; break;
- case '-': cout << "Result: " << a - b << endl; break;
- case '*': cout << "Result: " << a * b << endl; break;
- case '/':
- if (b != 0) {
- cout << "Result: " << a / b << endl;
- } else {
- cout << "Cannot divide by zero" << endl;
- }
- break;
- default:
- cout << "Unknown operator " << op << endl;
- }
a and b are double, so 7 / 2 = 3.5.
Module 1 · The switch Statement
Programming Fundamentals · Lecture 10
Slide 10
Forgetting break: Fall-Through
Without break, C++ keeps running into the next cases
fallthrough.cpp — no break!
- Why?
- case labels are only entry points. Nothing stops the program until break or the closing }.
- int choice = 2;
- switch (choice) {
- case 1: cout << "Tea" << endl;
- case 2: cout << "Coffee" << endl;
- case 3: cout << "Juice" << endl;
- default: cout << "Water" << endl;
- }
- Fix
- End every case with break;
Output — ordered coffee, got three drinks
g++ -Wextra: warning: this statement may fall through
- Coffee
- Juice
- Water
Module 2 · break, Fall-Through & Limits
Programming Fundamentals · Lecture 10
Slide 11
Using Fall-Through on Purpose: Grouping Cases
Several labels can share one block
vowel.cpp (decision part)
Run 1
- switch (ch) {
- case 'a':
- case 'e':
- case 'i':
- case 'o':
- case 'u':
- cout << ch << " is a vowel" << endl;
- break;
- default:
- cout << ch << " is a consonant" << endl;
- }
- Enter a small letter: e
- e is a vowel
Run 2
- Enter a small letter: k
- k is a consonant
- Real-world
- Saturday and Sunday both → "Weekend": case 6: case 7: cout << "Weekend";
Module 2 · break, Fall-Through & Limits
Programming Fundamentals · Lecture 10
Slide 12
What switch Cannot Do
Real compiler messages
err_string.cpp
Trick: grade_switch.cpp (bands of 10)
- string city = "Lahore";
- switch (city) { case "Lahore": … }
- switch (marks / 10) { // 87 / 10 = 8
- case 10:
- case 9:
- case 8: cout << "Grade A"; break;
- case 7: cout << "Grade B"; break;
- case 6: cout << "Grade C"; break;
- case 5: cout << "Grade D"; break;
- default: cout << "Grade F";
- }
g++ says
error: switch quantity not an integer
err_range.cpp
switch (marks) { case marks >= 50: … }
g++ says
Tested: 100, 87 → A · 72 → B · 65 → C · 50 → D · 34 → F. Works only when limits are multiples of 10.
- error: the value of 'marks' is not
- usable in a constant expression
Module 2 · break, Fall-Through & Limits
Programming Fundamentals · Lecture 10
Slide 13
switch or else-if Ladder?
Pick the tool that fits
- Check yourself
- Grade from marks with limits 85/70/60/50 → ladder. ATM menu 1–4 → switch. Temperature warning (double) → ladder.
Module 2 · break, Fall-Through & Limits
Programming Fundamentals · Lecture 10
Slide 14
The Conditional Operator ?:
A one-line if-else that gives a VALUE
condition ? value if true : value if false
ternary.cpp
Output
- int a = 14, b = 9, n = 7;
- bool isStudent = true;
- int larger = (a > b) ? a : b;
- cout << "Larger: " << larger << endl;
- cout << n << " is " << (n % 2 == 0 ? "even" : "odd") << endl;
- int ticket = isStudent ? 500 : 800;
- cout << "Ticket: Rs. " << ticket << endl;
- Larger: 14
- 7 is odd
- Ticket: Rs. 500
- Same as
- if (a > b) larger = a;
- else larger = b;
Module 3 · The Conditional Operator ?:
Programming Fundamentals · Lecture 10
Slide 15
When to Use ?: — and When Not To
Good for a simple choice between two values
- Good
- fee = isHostelite ? 18000 : 0;
- cout << (passed ? "PASS" : "FAIL");
- days = isLeap ? 29 : 28;
- Avoid
- g = m >= 85 ? 'A' : m >= 70 ? 'B' : m >= 60 ? 'C' : 'F';
- Correct, but hard to read → use a ladder.
Module 3 · The Conditional Operator ?:
Programming Fundamentals · Lecture 10
Slide 16
CLASS ACTIVITY · 8 MIN · IN PAIRS
Phone-Banking Menu
Write a switch for this menu. Then rewrite the two if-else statements on the right using ?:
Menu (int choice)
Rewrite with ?:
- 1 → "Your balance is Rs. 12,000"
- 2 → "Last 5 transactions…"
- 3 → "Transfer money"
- 4 → "Thank you, goodbye"
- anything else → "Invalid choice"
- What happens if you forget break after case 3 and the user presses 3?
- a) if (age < 12) fare = 600;
- else fare = 1200;
- b) if (n >= 0) cout << "non-negative";
- else cout << "negative";
Slide 17
Quick Recap
Answer aloud
Q1
What does break do inside a switch?
Q2
Can you switch on a double or a string? What does g++ say?
Q3
When is a switch better than an else-if ladder?
Q4
Write max = (x > y) ? x : y; as an if-else.
Programming Fundamentals · Lecture 10
Slide 18
Homework
Month number (1–12) → number of days. Group the months with fall-through (case 1: case 3: …). February = 28.
Convert the grade ladder from Lecture 9 into a switch using marks / 10 (bands of 10). Test 100, 80, 79, 49.
Use ?: to print "Leap year" / "Not a leap year" for an input year. Test 2024 and 1900.
Explain in two lines why case marks >= 50: does not compile.
Next lecture: Repetitive statements I — the while loop (doing things again and again)