On this page11 sections
Lecture 016: else-if Ladder & Nested if
| Day | 6: if Statements |
| Lecture | 016 of 057 |
| Estimated video length | 12 min |
| Target | Dart 3.12 |
| Level | Beginner |
Real decisions often have more than two outcomes: a grade can be A, B, C or F. An else-if ladder checks one condition after another. And sometimes a decision depends on an earlier one, which is when you put an
ifinside anif.
Contents
Prerequisites · Objectives · Lesson · Code Examples · Common Mistakes · Practice Questions · Answers · Takeaways
Prerequisites
Lecture 015 (operators, if and if-else).
Learning Objectives
- Choose between many outcomes with an else-if ladder
- Order the conditions correctly
- Put an
ifinside anotherif(nested if) - Flatten nested ifs with
&&when it reads better
1. The else-if Ladder
if (first condition) {
...
} else if (second condition) {
...
} else if (third condition) {
...
} else {
// none of the above
}Dart checks the conditions from the top. The first true one runs, and the rest are
skipped. The final else is optional and catches everything left over.
var marks = 74;
if (marks >= 80) {
print('Grade A');
} else if (marks >= 70) {
print('Grade B');
} else if (marks >= 60) {
print('Grade C');
} else {
print('Grade F');
}
// Grade B74 >= 80 is false, 74 >= 70 is true, so Grade B prints and Dart jumps past the rest.
2. Order Matters
Because the first match wins, put the strictest condition first:
var wrong = 95;
if (wrong >= 50) {
print('wrong order: D');
} else if (wrong >= 80) {
print('wrong order: A');
}
// wrong order: D95 deserves an A, but 95 >= 50 matched first and the A check never ran.
3. Nested if
An if inside another if handles a question that only makes sense after the first answer:
var age = 20;
var hasId = false;
if (age >= 18) {
if (hasId) {
print('You can vote.');
} else {
print('Bring your ID card.');
}
} else {
print('Too young to vote.');
}
// Bring your ID card.The ID question is only asked of people who are old enough.
4. Flattening With &&
Deep nesting gets hard to read. The same logic as a ladder:
if (age >= 18 && hasId) {
print('You can vote.');
} else if (age >= 18) {
print('Bring your ID card.');
} else {
print('Too young to vote.');
}Both versions are correct. Pick the one that's easier to follow, and avoid nesting more than two or three levels.
Code Examples
Example 1: An Electricity Bill
void main() {
var units = 320;
var isCommercial = false;
var bill = 0.0;
if (units <= 100) {
bill = units * 10.0;
} else if (units <= 200) {
bill = 100 * 10.0 + (units - 100) * 15.0;
} else {
bill = 100 * 10.0 + 100 * 15.0 + (units - 200) * 22.0;
}
if (isCommercial) {
bill = bill * 1.2;
}
var status = '';
if (bill > 4000) {
status = 'High usage';
if (units > 300) {
status = 'Very high usage';
}
} else {
status = 'Normal usage';
}
print('Units: $units');
print('Bill: Rs $bill');
print('Status: $status');
}Output
Units: 320
Bill: Rs 5140.0
Status: Very high usageThe first 100 units cost Rs 10 each, the next 100 cost Rs 15, and everything above 200 costs
Rs 22: 1000 + 1500 + 120 × 22 = 5140. Try units = 150: the bill becomes Rs 1750.0 and the
status Normal usage.
Common Beginner Mistakes
1. Giving else a condition
} else (x > 1) {Error: Expected ')' before this.else never has a condition. Write else if (x > 1).
2. Wrong order in the ladder: >= 50 before >= 80 means nobody ever gets an A. Go from
strictest to loosest.
3. Separate ifs instead of a ladder: three separate if (marks >= 80), if (marks >= 70),
if (marks >= 60) statements can all run for 85 marks. Use else if when only one outcome
should happen.
4. Nesting too deep: four or five levels of if inside if is hard to follow. Combine
conditions with &&, or split the logic up (functions, Day 10).
Practice Questions
- In an else-if ladder, how many blocks can run?
- With the grade ladder from Section 1, what prints for
marks = 60? Formarks = 59? - Fix the order:
if (x >= 10) ... else if (x >= 100) ... - Write a ladder that prints
Child(under 13),Teen(13–19) orAdult(20+) forage. - Rewrite the voting example's nested
ifusing&&.
Practice Answers
- At most one, the first whose condition is true (or the
else). Grade C;Grade F.- Check
x >= 100first, thenx >= 10.
if (age < 13) {
print('Child');
} else if (age < 20) {
print('Teen');
} else {
print('Adult');
}- See Section 4:
if (age >= 18 && hasId) ... else if (age >= 18) ... else ...
Key Takeaways
- An else-if ladder checks conditions top to bottom; the first true one wins.
- Put the strictest condition first.
elsehas no condition and catches everything else.- Nested ifs ask a follow-up question; flatten them with
&&when that reads better.