DEV Community

Cover image for Else if Ladder Statement in Dart
Jay Tillu
Jay Tillu

Posted on • Edited on

Else if Ladder Statement in Dart

Else if ladder is a type of conditionals in the dart. Just like if and if-else. We use else if ladder when we want to check multiple conditions.

Syntax


if (boolean_expression) {
  //Statements to be executed if the condition is true
} else if (boolean_expression) {
  //Statements to be executed if the condition is true
} else if (boolean_expression) {
  //Statements to be executed if the condition is true
} else {
  //Statements to be executed if all the conditions are false
}
Enter fullscreen mode Exit fullscreen mode
  • If the Boolean expression evaluates to be true, then the block of code inside the if statement will be executed. Otherwise, the compiler will check else-if’s condition.

  • Once any else-if’s condition is true, then all other else-if conditions will be skipped. So every else-if condition must be unique.

  • If all the else-if conditions evaluate to be false, then the else’s code block will be executed.

  • Here again, the else is optional. But if it occurs then it must be after all the else-if blocks.

Sample Code

void main() {
  int number = 2;

  if (number == 5) {
    print("Number is 5");
  } else if (number == 1) {
    print("Number is 1");
  } else if (number == 2) {
    print("Number is 2");
  } else {
    print("None of the number is matched");
  }
}

Output
Number is 2
Enter fullscreen mode Exit fullscreen mode

That’s all for else if ladder statement guys. Feel free to share me if I miss something. I’ll love to learn it from you. Till Then Keep Loving, Keep Coding. I’ll surely catch you up in the next article.

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitate that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

Learn more about Dart and Flutter

Follow me for more such content

Top comments (0)