Summary: in this tutorial, you’ll learn how to use the Dart if else
statement to do something when a condition is true
and another thing when the condition is false
.
Introduction to the Dart if else statement
In the previous tutorial, you learned how to do something if a condition is true
by using the if
statement. In practice, you often want to do something if a condition is true
and do another thing if the condition is false
. The Dart if else
statement enables this.
Here’s the syntax of the if else
statement:
if (condition)
{
// statements
}
else
{
// statements
}
Code language: Dart (dart)
In this syntax, the if else
statement evaluates the condition
first.
If the condition
is true
, it’ll then execute the block within the curly braces that follow the if
clause. If the condition is false
, the if else
statement executes the statement in the else
block.
The following flowchart illustrates how the if else
statement works:
Dart if else statement examples
Let’s take some examples of using the if else
statement to understand how it works.
1) Simple Dart if else statement examples
The following example uses the if else
statement to show a message based on the value of the isWeekend
variable:
void main() {
bool isWeekend = true;
if (isWeekend) {
print("Let's play!");
} else {
print("Let's go to work!");
}
}
Code language: Dart (dart)
Output:
Let's play!
Code language: Dart (dart)
In this example, the isWekeend
is true
. Therefore, the program displays the message "Let's play!"
.
In the following, we change the value of the isWeekend
variable to false
:
void main() {
bool isWeekend = false;
if (isWeekend) {
print("Let's play!");
} else {
print("Let's go to work!");
}
}
Code language: Dart (dart)
Output:
Let's go to work!
Code language: Dart (dart)
Since the expression isWeekend
is false
, the else
block executes. Therefore, the program shows the message "Let's go to work!"
.
2) Using Dart if else statement with a complex condition example
The following example uses the if else
statement with a complex condition:
void main() {
bool isWeekend = true;
String weather = "sunny";
if (isWeekend && weather == "sunny") {
print("Let's go to the beach!");
} else {
print("Let's go to work!");
}
}
Code language: Dart (dart)
Output:
Let's go outside.
Code language: Dart (dart)
In this example, we set the isWeekend
to true
and weather
to sunny
. Therefore, the following expression evaluates to true
:
isWeekend && weather == "sunny"
Code language: Dart (dart)
Hence, the if
block executes that shows the message "Let's go to the beach!"
.
In the following example, we change the isWeekend
to false
:
void main() {
bool isWeekend = false;
String weather = "sunny";
if (isWeekend && weather == "sunny") {
print("Let's go to the beach!");
} else {
print("Let's go to work!");
}
}
Code language: Dart (dart)
Output:
Let's go to work!
Code language: Dart (dart)
Because the following expression evaluates to false
, the else
clause executes that displays the message "Let's go to work!"
:
isWeekend && weather == "sunny"
Code language: JavaScript (javascript)
Summary
- Use Dart
if else
statement to do something if a condition istrue
and do another thing if the condition isfalse
.