Dart If...Else
Dart If...Else
A program often has to make a choice.
The if statement lets Dart run a block of code only when a condition is true.
A condition is an expression that gives a bool value, true or false.
You often build conditions with the comparison operators, like <, >, == and !=.
Dart has these conditional statements:
ifruns a block of code when a condition is trueelseruns a block of code when the same condition is falseelse iftests a new condition when the first condition is falseswitchpicks one of many blocks of code (see the next chapter)
The if Statement
Write the condition in parentheses () after if.
Write the code to run in curly brackets {}.
if (condition) {
// code to run if the condition is true
}
Here we test if 20 is greater than 18:
Result:
20 is greater than 18
You can also test variables:
Example
void main() {
final x = 20;
final y = 18;
if (x > y) {
print('x is greater than y');
}
}
Try it Yourself »
Result:
x is greater than y
The condition must be a bool.
Dart does not treat numbers or strings as true or false, so if (1) does not work:
Example
void main() {
final count = 1;
if (count) {
print('count is not zero');
}
}
Try it Yourself »
Result:
main.dart:3:7: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
if (count) {
^
The else Statement
Use else to run a block of code when the condition is false:
Example
void main() {
final time = 20;
if (time < 18) {
print('Good day.');
} else {
print('Good evening.');
}
}
Try it Yourself »
Result:
Good evening.
The value of time is 20, so the condition time < 18 is false.
Dart skips the first block and runs the else block.
The else if Statement
Use else if to test a new condition when the first condition is false.
Dart tests the conditions from top to bottom and runs only the first block whose condition is true:
Example
void main() {
final time = 16;
if (time < 10) {
print('Good morning.');
} else if (time < 18) {
print('Good day.');
} else {
print('Good evening.');
}
}
Try it Yourself »
Result:
Good day.
You can use as many else if parts as you need:
Example
void main() {
final score = 72;
if (score >= 90) {
print('Grade A');
} else if (score >= 80) {
print('Grade B');
} else if (score >= 70) {
print('Grade C');
} else {
print('Grade F');
}
}
Try it Yourself »
Result:
Grade C
Nested If
You can put an if statement inside another if statement.
This is called a nested if:
Example
void main() {
final age = 25;
final hasTicket = true;
if (age >= 18) {
print('You are an adult.');
if (hasTicket) {
print('You can enter the concert.');
} else {
print('You need a ticket.');
}
}
}
Try it Yourself »
Result:
You are an adult.
You can enter the concert.
You can often write the same check with the logical operator && instead:
Example
void main() {
final age = 25;
final hasTicket = true;
if (age >= 18 && hasTicket) {
print('You can enter the concert.');
}
}
Try it Yourself »
Result:
You can enter the concert.
The Conditional Expression (Ternary)
Dart has a short way to choose between two values.
It is called the conditional expression, or the ternary operator, because it has three parts:
condition ? valueIfTrue : valueIfFalse
Instead of writing this:
Example
void main() {
final time = 20;
String greeting;
if (time < 18) {
greeting = 'Good day.';
} else {
greeting = 'Good evening.';
}
print(greeting);
}
Try it Yourself »
Result:
Good evening.
You can simply write:
Example
void main() {
final time = 20;
final greeting = time < 18 ? 'Good day.' : 'Good evening.';
print(greeting);
}
Try it Yourself »
Result:
Good evening.
Tip: Use the conditional expression for short, simple choices.
For longer logic, an if...else statement is easier to read.