The else and else if Statement

Let’s learn to refine conditional statements using else and else if in Dart.

Overview

Imagine you’re playing a game of basketball. When the game ends, to decide who won, the referee counts the total points made by each team. If team A made a greater number of points, they would be declared the winner, else Team B would be declared the winner.

Do you see how we have two different actions depending on the outcome?

If we want to write some code for the above scenario, the first thing that comes to mind is the if statement. But the if statement won’t be able to do the job on its own because it only caters to one condition and one outcome. Our scenario has two outcomes.

The else Statement

Dart provides an else statement. else is used with an if statement and tells the compiler what to do if the if condition is not fulfilled.

Control Flow

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy