Dart If
Dart If is a simple conditional statement where a block of statements get executed if the given boolean expression evaluates to true.
Syntax
The syntax of if statement in Dart is shown below.
if (boolean_expression) {
//statement(s)
}
where the boolean_expression
evaluates to, or promotes to boolean value: true
or false
.
If the boolean_expression is true, statement(s) get executed.
If the boolean_expression is false, statement(s) does not execute and continues with the statements after if statement.
Example
In the following example, we will check if the given number in x
is even.
main.dart
void main(){
int x = 8;
if(x%2==0){
print('$x is even number.');
}
}
Output
10 is even number.
Now, we will change the value in x
to 7, and execute the same if-statement as in the above program.
main.dart
void main(){
int x = 7;
if(x%2==0){
print('$x is even number.');
}
}
Output
Since the condition in if-statement evaluates to false, the statement(s) inside if-block are not executed.
Conclusion
In this Dart Tutorial, we learned about Dart If-Statement, its syntax and usage, with examples.