Like any language, Dart also supports if, if else, if else conditional decisive statements.
It provides the following features
- simple if statements
- if else statements
- if else if else
Dart simple if statements
Simple if is a code block executes if the conditional expression is true. The conditional expression always results in the bool type and the possible values - true and false only. Here is the syntax of a simple if statement.
if (conditional-expression){
//if code block executed
}
if
is a keyword in a dart that can not be used as variable names in a dart.
conditional-expression is always resulting intrue
or false
only. if it is true code block executed. the conditional expression must enclose in (()
).
code block contains a single line or block(multiple lines) of code.
Multiple lines of code must be enclosed in {}
, and it is optional for single lines of code.
Simple if statements
are used to test a single possible value like null check or empty object check.
Here are valid if simple statements examples
void main() {
bool enabled = true;
// It is optional to include {} for single line of code
if (true) print("always true");
// multiple lines inside a code block must enclose {}
if (enabled) {
print("enabled=true");
print("enable value is displayed");
}
}
Output:
always true
enabled=true
enable value is displayed
The above dart code prints
Invalid if statements that throw Error: A value of type ‘int’ can’t be assigned to a variable of type ‘bool’.
void main() {
int i = 10;
// compilation error conditional value must be bool type
if (i) {
print("compiled failed");
}
}
Dart if else statements example
if else
is to execute multiple blocks of code based on conditional expression value.
The conditional expression always results in the bool type and the possible values - true and false only.
if else statements are used to execute statements based on true and false values.
Here is the syntax of the if else statement.
if (conditional-expression1){
//statements1
}
else {
//statements2
}
conditional-express results in a value of bool type.
if true, statements1 inside an if block is executed, else statements2 are executed.
Here is an example code
void main() {
int salary = 5000;
if (salary > 5000) {
print("salary is greater than 5000");
} else {
print("salary is less than 5000");
}
}
Dart if else if statements example
if else if
is to execute multiple blocks of code based on multiple conditional expression values.
The conditional expression always results in the bool type and the possible values - true and false only.
if else
statements are used to execute multiple conditional statements based on true and false values.
Here is the syntax of the if else if else statement.
if (condition1) {
// statements executes if condition1 is true
} else if (condition2) {
// statements executes if condition1 is false and condition2 is true
} else {
// statements executes if condition1 is false and condition2 is false
}
Here is an example
void main() {
int number = 50;
if (number == 50) {
print("50");
} else if (number == 100) {
print("100");
} else if (number == 25) {
print("25");
} else {
print("other number");
}
}
In the above example, if the number is 50, print 50 to console else, if the number is 100, print 100. else if the number is 25, print 25 if none of the conditions are met, It prints “other number”.