Else-If Ladder in Dart
The else-if ladder allows you to test multiple conditions one after another. The program executes the block of the first true condition and skips the rest.
Syntax:
if (condition1) {
// code if condition1 is true
} else if (condition2) {
// code if condition2 is true
} else if (condition3) {
// code if condition3 is true
} else {
// code if all conditions are false
}
Example:
void main() {
int score = 72;
if (score >= 90) {
print('Grade: A');
} else if (score >= 75) {
print('Grade: B');
} else if (score >= 60) {
print('Grade: C');
} else {
print('Grade: F');
}
}
Explanation:
- The program checks conditions in order.
- When it finds a condition that is true, it runs that block and skips the rest.
Sample Input/Output
Input:
score = 95
Output:
Grade: A
Input:
score = 72
Output:
Grade: C
Input:
score = 40
Output:
Grade: F