Skip to main content
The else if statement is a conditional control flow mechanism that enables the sequential evaluation of multiple boolean expressions. It allows the program to test a new condition only if the preceding if statement (and any preceding else if statements) evaluated to false.

Syntax

The else if clause is syntactically positioned after an if block and before an optional else block.
if (condition1) {
  // Executes if condition1 is true
} else if (condition2) {
  // Executes if condition1 is false and condition2 is true
} else if (condition3) {
  // Executes if condition1 and condition2 are false, and condition3 is true
} else {
  // Executes if all preceding conditions are false
}

Execution Logic

  1. Sequential Evaluation: The Dart runtime evaluates conditions in a strict top-down order.
  2. Short-Circuiting: Once a condition evaluates to true, the corresponding code block is executed, and the entire conditional chain is terminated. Subsequent else if or else blocks are ignored, even if their conditions would have also been true.
  3. Type Requirement: Unlike loosely typed languages, Dart requires the condition within the parentheses to evaluate strictly to a bool type. It does not perform implicit truthy/falsy conversion.

Scope

Each else if statement defines its own block scope. Variables declared within the curly braces {} of an else if block are local to that block and are not accessible to the surrounding scope or subsequent conditional blocks.
Master Dart with Deep Grasping Methodology!Learn More