On this page11 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. The else-if Ladder
  4. 2. Order Matters
  5. 3. Nested if
  6. 4. Flattening With &&
  7. Code Examples
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 016: else-if Ladder & Nested if

Day6: if Statements
Lecture016 of 057
Estimated video length12 min
TargetDart 3.12
LevelBeginner

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 if inside an if.

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 if inside another if (nested if)
  • Flatten nested ifs with && when it reads better

1. The else-if Ladder

dart
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.

dart
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 B

74 >= 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:

dart
var wrong = 95;
if (wrong >= 50) {
  print('wrong order: D');
} else if (wrong >= 80) {
  print('wrong order: A');
}
// wrong order: D

95 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:

dart
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:

dart
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

dart
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

code
Units: 320
Bill: Rs 5140.0
Status: Very high usage

The 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

dart
} else (x > 1) {
code
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

  1. In an else-if ladder, how many blocks can run?
  2. With the grade ladder from Section 1, what prints for marks = 60? For marks = 59?
  3. Fix the order: if (x >= 10) ... else if (x >= 100) ...
  4. Write a ladder that prints Child (under 13), Teen (13–19) or Adult (20+) for age.
  5. Rewrite the voting example's nested if using &&.

Practice Answers

  1. At most one, the first whose condition is true (or the else).
  2. Grade C; Grade F.
  3. Check x >= 100 first, then x >= 10.
dart
if (age < 13) {
  print('Child');
} else if (age < 20) {
  print('Teen');
} else {
  print('Adult');
}
  1. 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.
  • else has no condition and catches everything else.
  • Nested ifs ask a follow-up question; flatten them with && when that reads better.