Control Flow: if, else, else if

on Saturday, 25th of July, 2020

The following is an excerpt from the book Flutter in Action.

Dart supports if, else if, and else, as you'd expect. Here's a standard if statement:

// this checks a single condition, 
// and the "else" block provides a fallback
if (inPortland) {
  print('Bring an umbrella!');
} else {
  print('Check the weather first!');
}

// using "else if" allows you to check for multiple conditions
// and if they all evaluate to false, fall back
if (inPortland && isSummer) {
  print('The weather is amazing!');
} else if(inPortland && isAnyOtherSeason) {
  print('Torrential downpour.');
} else {
  print ('Check the weather!');
}

// you can use as many "else if" statements as you need to
if (inPortland && isSummer) {
  print('The weather is amazing!');
} else if(inPortland && isSpring) {
  print('Torrential downpour.');
} else if(inPortland && isAutumn) {
  print('Torrential downpour.');
} else if(inPortland && isWinter) {
  print('Torrential downpour.');
} else {
  print ('Check the weather!');
}
Note that the `else if` statements execute in the order that they're written. The first one that evaluates to "true" is the only code block that is executed.

Finally, Dart is sane, and a condition must evaluate to a boolean. There is only one way to say "true" (true) and one way to say "false" (false). In some languages, there is a concept of "truthiness," and all values coerce to true or false. In such languages, you can write if (3) {, and it works. That is not the case in Dart.



Join thousands of Flutter developers.

Sign up for infrequent updates about Flutter and Dart.

You can get all this content and more in one place. Check out my new book Flutter in Action