UNIVERSITY OF AGRICULTURE, FAISALABAD
Department of Computer Science
LECTURE 9 · STUDENT NOTES
Conditional Statements II: Nested if & the else-if Ladder
CS-307 Programming Fundamentals · BSCS Semester 1
Week | Slides | Linked practical | Reference ID |
4 | Lecture9_Nested_If_Else_If_Ladder.pptx | Practical 7 | N09 |
Learning Outcomes
SLO | By the end of this lecture you can… | Builds CLO | Bloom level |
|---|---|---|---|
SLO 9.1 | Trace the flow of execution through nested if statements | CLO4: Describe the syntax and semantics of the C++ programming language. | C3 |
SLO 9.2 | Write an else-if ladder to choose one of many options (grades, bill slabs) | CLO4: Describe the syntax and semantics of the C++ programming language. | C3 |
SLO 9.3 | Order conditions correctly and find overlapping-range and dangling-else mistakes | CLO1: Identify the necessary properties of good problem-solving techniques. | C4 |
How to use these notes Read these notes after the lecture and before the practical. Section numbers (for example §3.2) are used in the question bank, so you can always find where an answer comes from. Type every code example yourself — do not copy-paste. |
1. More Than Two Choices
An if-else (Lecture 8) gives two paths. Many problems need more. There are two common shapes: (Slide L09-S05)
Shape | Use when | Examples |
|---|---|---|
Nested if | a second question only matters after the first is answered | admission (FSc, then entry test); mobile login (SIM, then PIN) |
else-if ladder | one value falls into one of many ranges | grade from marks; electricity bill slab; courier weight |
2. Nested if
2.1 A decision inside a decision
Nested means placed inside. In a nested if, an inner if is written inside the block of an outer if. The inner condition is checked only when the outer condition is true. (Slide L09-S06)
Shape of a nested if if (outer condition) { if (inner condition) { // both true } else { // outer true, inner false } } else { // outer false } |
Figure 9.1 – Nested if: the entry test is checked only after the FSc condition is true
2.2 Example: university admission
Rule (example): a student needs at least 60% in FSc; an eligible student is admitted if the entry test mark is 50 or more. (Slide L09-S07)
admission.cpp #include <iostream> using namespace std;
int main() { double fsc, test; 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; } return 0; } |
fsc | test | Path | Output |
|---|---|---|---|
72 | 65 | outer true → inner true | Admitted |
72 | 40 | outer true → inner false | Eligible, but failed the entry test |
55 | 90 | outer false (inner never checked) | Not eligible (FSc below 60%) |
2.3 Example: largest of three numbers
(Slide L09-S08)
largest.cpp #include <iostream> using namespace std;
int main() { int a, b, c; cout << "Enter three numbers: "; cin >> a >> b >> c;
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; } } return 0; } |
a b c | Path | Output |
|---|---|---|
9 2 5 | a >= b true → a >= c true | Largest: 9 |
3 8 8 | a >= b false → b >= c true | Largest: 8 |
1 4 7 | a >= b false → b >= c false | Largest: 7 |
5 5 1 | a >= b true → a >= c true | Largest: 5 |
Always include equal values in your tests (3 8 8, 5 5 1).
3. The else-if Ladder
3.1 Syntax and how it runs
An else-if ladder is a chain of conditions. C++ checks them from top to bottom; the first true condition's block runs and all the rest are skipped. The final else (optional) runs only when nothing matched. (Slides L09-S09, S11)
Syntax if (condition1) { // block 1 } else if (condition2) { // block 2 } else if (condition3) { // block 3 } else { // none matched } |
Figure 9.2 – An else-if ladder: the first true condition wins
3.2 Example: grade from marks
Example grading scheme (for teaching): A 85+, B 70–84, C 60–69, D 50–59, F below 50. (Slide L09-S10)
grade.cpp #include <iostream> using namespace std;
int main() { int marks; cout << "Enter marks (0-100): "; cin >> marks;
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; } return 0; } |
marks | 92 | 85 | 84 | 63 | 50 | 49 |
|---|---|---|---|---|---|---|
Output | A | A | B | C | D | F |
We write marks >= 70, not marks >= 70 && marks < 85: reaching the second test already means marks is below 85.
3.3 Trace for marks = 63
Step | Condition | Result | What happens |
|---|---|---|---|
1 | marks >= 85 | false | go down |
2 | marks >= 70 | false | go down |
3 | marks >= 60 | true | print Grade C |
4 | marks >= 50 | not checked | skipped |
5 | else | not reached | skipped |
Real-world: sorting eggs by size — try the biggest tray first; each egg goes into the first tray it fits.
3.4 Example: electricity bill by slab
Example rates (not the real tariff), with the whole bill at one rate: up to 100 units Rs. 10, 101–200 Rs. 15, 201–300 Rs. 20, above 300 Rs. 25. (Slide L09-S12)
slab.cpp #include <iostream> using namespace std;
int main() { int units; double rate; cout << "Units used: "; cin >> units;
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; return 0; } |
units | 80 | 100 | 150 | 250 | 420 |
|---|---|---|---|---|---|
rate | 10 | 10 | 15 | 20 | 25 |
Bill (Rs.) | 800 | 1000 | 2250 | 5000 | 10500 |
3.5 Nested if or ladder?
(Slide L09-S13) One variable split into ranges → ladder. A second question that matters only after the first → nested if. Deep nesting (three or more levels) is hard to read; if the inner decisions are just ranges of one value, rewrite them as a ladder.
4. Order of Conditions and the Dangling else
4.1 Order matters
Because the first true condition wins, the order of a ladder decides the result. (Slide L09-S14)
grade_wrong.cpp #include <iostream> using namespace std;
int main() { 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; } else { cout << "Grade F" << endl; } return 0; } |
Output (wrong — 92 should be A) Grade D |
92 >= 50 is true at the first step, so the ladder stops. A, B and C can never be printed. There is no compiler warning — it is a logic error.
Rule Ladder with >= (going down): start from the largest limit (85, 70, 60, 50). Ladder with <= (going up): start from the smallest limit (100, 200, 300). Put validity checks (e.g. weight <= 0 is invalid) first. |
4.2 The dangling else
Without braces, an else belongs to the nearest unmatched if above it — not to the if it is lined up with. (Slide L09-S15)
dangling.cpp #include <iostream> using namespace std;
int main() { 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; return 0; } |
Output — 'a is not positive' is missing Done |
The else is attached to if (b > 0). Since a = -1, the outer if is false and nothing inside it runs. g++ -Wall warns: suggest explicit braces to avoid ambiguous 'else'. Fix with braces:
Fixed if (a > 0) { if (b > 0) { cout << "Both positive" << endl; } } else { cout << "a is not positive" << endl; } |
5. Word Bank (Key Terms)
Word | Simple meaning |
|---|---|
Nested | Placed inside another (an if inside an if) |
Outer / inner if | The containing if / the if inside it |
else-if ladder | A chain of if … else if … else to choose one of many options |
Slab | A range of values with its own rate |
Unreachable code | Code that can never run because of the conditions before it |
Dangling else | An else that could belong to two ifs; C++ gives it to the nearest one |
Boundary value | A test value exactly at a limit (85, 84, 50, 49) |
6. Self-Check Questions
1. When is the inner condition of a nested if checked?
2. In an else-if ladder, how many blocks can run? What if there is no final else and nothing matches?
3. Trace grade.cpp for marks = 70 and 69.
4. Why does the ladder in grade_wrong.cpp print D for 92? How do you fix it?
5. Which if does an else belong to when there are no braces?
6. Write an else-if ladder for courier charges: ≤ 1 kg Rs. 250, ≤ 5 kg Rs. 600, ≤ 10 kg Rs. 1,000, above → "Send by cargo", and ≤ 0 → "Invalid weight".
Answers are in §2.1, §3.1, §3.2 (70 → B, 69 → C), §4.1, §4.2 and the Lecture 9 teaching guide. Homework is on Slide L09-S18.