Slide 2
Quick Recap of Lecture 8
5 minutes · homework check
Homework 3
Remember
- if (units > 200) {
- cout << "Not protected";
- } else {
- cout << "Protected consumer";
- }
- // tests: 199, 200, 201
- if: the block runs only when true
- if-else: exactly one block runs
- Always use braces { }
- No ; after the condition
- Test both branches and the boundary
- Today's question
- What if there are MORE than two choices — like grades A, B, C, D, F?
Recap
Programming Fundamentals · Lecture 9
Slide 3
Today's Plan
50 minutes · 8 parts
Recap & homework check
5 min
Learning outcomes & many choices
3 min
Nested if: a decision inside a decision
10 min
The else-if ladder
12 min
Order of conditions & the dangling else
6 min
Class activity: courier charges
8 min
Recap quiz
5 min
Homework & next lecture
1 min
Programming Fundamentals · Lecture 9
Slide 4
Learning Outcomes (SLOs)
By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)
Trace the flow of execution through nested if statements
SLO 9.1 → CLO4 · C3
Write an else-if ladder to choose one of many options (grades, bill slabs)
SLO 9.2 → CLO4 · C3
SLO 9.3 → CLO1 · C4
Order conditions correctly and find overlapping-range and dangling-else mistakes
- CLO1 Identify the necessary properties of good problem-solving techniques.
- CLO4 Describe the syntax and semantics of the C++ programming language.
Programming Fundamentals · Lecture 9
Slide 5
Life Has More Than Two Choices
One if-else gives two paths. Many problems need more.
Admission
Mobile login
First: FSc ≥ 60%? Only then: entry test ≥ 50?
First: SIM correct? Only then: PIN correct?
nested if
nested if
Result card
Electricity bill
A, B, C, D or F — from one marks value
Rate depends on which slab the units fall in
else-if ladder
else-if ladder
Module 1 · Nested if
Programming Fundamentals · Lecture 9
Slide 6
Nested if: A Decision Inside a Decision
The inner if is checked ONLY when the outer condition is true
fsc >= 60?
Shape of a nested if
false
- if (outer condition) {
- if (inner condition) {
- // both true
- } else {
- // outer true, inner false
- }
- } else {
- // outer false
- }
true
test >= 50?
false
true
Admitted
Not eligible
The entry test is only checked for students who passed the FSc condition.
Failed test
Module 1 · Nested if
Programming Fundamentals · Lecture 9
Slide 7
Example: University Admission
Outer: FSc percentage · Inner: entry test
admission.cpp
- cout << "FSc percentage: "; cin >> fsc;
- cout << "Entry test marks (out of 100): "; cin >> test;
- if (fsc >= 60) {
- if (test >= 50) {
- cout << "Admitted" << endl;
- } else {
- cout << "Eligible, but failed the entry test" << endl;
- }
- } else {
- cout << "Not eligible (FSc below 60%)" << endl;
- }
- Notice
- 55 / 90: the inner if never runs. Order of checks follows the real rule.
Module 1 · Nested if
Programming Fundamentals · Lecture 9
Slide 8
Example: Largest of Three Numbers
Nested if-else in both branches
largest.cpp (decision part)
- if (a >= b) {
- if (a >= c) {
- cout << "Largest: " << a << endl;
- } else {
- cout << "Largest: " << c << endl;
- }
- } else {
- if (b >= c) {
- cout << "Largest: " << b << endl;
- } else {
- cout << "Largest: " << c << endl;
- }
- }
- Test equal values
- Inputs like 3 8 8 and 5 5 1 check the paths where two numbers are the same. Both give the right answer here.
Module 1 · Nested if
Programming Fundamentals · Lecture 9
Slide 9
The else-if Ladder
Check conditions from top to bottom — the FIRST true one wins
cond 1?
Syntax
true
block 1
- if (condition1) {
- // block 1
- } else if (condition2) {
- // block 2
- } else if (condition3) {
- // block 3
- } else {
- // none matched
- }
false
cond 2?
true
block 2
false
cond 3?
true
block 3
Exactly ONE block runs — the first one whose condition is true.
else block
false
→ next statement
Module 2 · The else-if Ladder
Programming Fundamentals · Lecture 9
Slide 10
Example: Grade from Marks
An example grading scheme: A 85+, B 70–84, C 60–69, D 50–59, F below 50
grade.cpp (decision part)
- if (marks >= 85) {
- cout << "Grade A" << endl;
- } else if (marks >= 70) {
- cout << "Grade B" << endl;
- } else if (marks >= 60) {
- cout << "Grade C" << endl;
- } else if (marks >= 50) {
- cout << "Grade D" << endl;
- } else {
- cout << "Grade F" << endl;
- }
Tested at every boundary: 85/84, 50/49.
Module 2 · The else-if Ladder
Programming Fundamentals · Lecture 9
Slide 11
How the Ladder Runs: Trace for marks = 63
Stop at the first true condition
Real-world: a shop sorting eggs by size: try the biggest tray first; an egg goes into the first tray it fits and is not tried again.
Module 2 · The else-if Ladder
Programming Fundamentals · Lecture 9
Slide 12
Example: Electricity Bill by Slab
The rate depends on which slab the units fall in (example rates)
slab.cpp (main part)
- if (units <= 100) {
- rate = 10;
- } else if (units <= 200) {
- rate = 15;
- } else if (units <= 300) {
- rate = 20;
- } else {
- rate = 25;
- }
- cout << "Rate: Rs. " << rate << " per unit" << endl;
- cout << "Bill: Rs. " << units * rate << endl;
Ladder going UP uses <= ; ladder going DOWN (grades) uses >=.
Module 2 · The else-if Ladder
Programming Fundamentals · Lecture 9
Slide 13
Nested if or else-if Ladder?
Choose the shape that matches the problem
- Readability tip
- Deep nesting (3+ levels) is hard to read. If the inner decisions are just ranges of one value, rewrite them as a ladder.
Module 2 · The else-if Ladder
Programming Fundamentals · Lecture 9
Slide 14
Order Matters: The Wrong Ladder
Everyone above 50 gets a D!
grade_wrong.cpp
- Why?
- 92 >= 50 is already true, so the ladder stops at the first step.
- A, B and C can never be printed.
- int marks = 92;
- if (marks >= 50) {
- cout << "Grade D" << endl;
- } else if (marks >= 60) {
- cout << "Grade C" << endl;
- } else if (marks >= 70) {
- cout << "Grade B" << endl;
- } else if (marks >= 85) {
- cout << "Grade A" << endl;
- }
- Fix
- >= ladder: biggest limit first (85, 70, 60, 50).
- <= ladder: smallest limit first (100, 200, 300).
Output
Grade D
Module 3 · Order & the Dangling else
Programming Fundamentals · Lecture 9
Slide 15
The Dangling else
An else belongs to the NEAREST unmatched if — not to the one it is lined up with
dangling.cpp
Fixed with braces
- int a = -1, b = 5;
- if (a > 0)
- if (b > 0)
- cout << "Both positive" << endl;
- else
- cout << "a is not positive" << endl;
- cout << "Done" << endl;
- if (a > 0) {
- if (b > 0) {
- cout << "Both positive" << endl;
- }
- } else {
- cout << "a is not positive" << endl;
- }
Output — the else message is missing!
Same lesson as Lecture 8: braces decide, indentation does not.
Done
g++ -Wall: suggest explicit braces to avoid ambiguous 'else'
Module 3 · Order & the Dangling else
Programming Fundamentals · Lecture 9
Slide 16
CLASS ACTIVITY · 8 MIN · IN PAIRS
Courier Charges
A courier company charges by parcel weight (example rates). Write the else-if ladder, then list the test values you would use.
Rules
Think
- • up to 1 kg: Rs. 250
- • more than 1 and up to 5 kg: Rs. 600
- • more than 5 and up to 10 kg: Rs. 1,000
- • more than 10 kg: "Send by cargo"
- • weight 0 or less: "Invalid weight" (check this FIRST)
- Going up — so use <= and start from the smallest limit.
- Which boundary values must you test?
Slide 17
Quick Recap
Answer aloud
Q1
In a nested if, when is the inner condition checked?
Q2
In an else-if ladder, how many blocks can run?
Q3
Why does a ladder starting with marks >= 50 give a D to everyone who passed?
Q4
Which if does an else belong to when there are no braces?
Programming Fundamentals · Lecture 9
Slide 18
Homework
Write grade.cpp with your own scheme and test it at every boundary.
Mobile data package: ≤ 2 GB Rs. 300, ≤ 5 GB Rs. 600, ≤ 10 GB Rs. 1,000, above Rs. 1,500 (example). Write the ladder.
Nested: login — if the username is "student", check PIN 1234 ("Welcome" / "Wrong PIN"); else "Unknown user".
Rewrite if (x > 0) if (y > 0) cout << "Q1"; else cout << "x not positive"; so the else belongs to the outer if.
Next lecture: Conditional statements III — switch and the conditional operator ?: