Dart if Statements
An if statement enables us to run a code block when the specified condition is satisfied. One common use case for Dart programming is the desire to run a piece of code when a certain condition is met. The decision is based on the Boolean values TRUE or FALSE that the condition evaluates.
Dart If Statement Flow Diagram
Syntax
If (condition) {
//statement(s)
}
The if statement must evaluate either TRUE or FALSE; if it does, a statement inside the if body will be executed; if it does not, a statement outside the if block will be run.
Example
void main () {
// define a variable which hold numeric value
var n = 35;
// if statement check the given condition
if (n<40){
print("The number is smaller than 40")
};
}
Output
The number is smaller than 40
Explanation
We defined the integer variable n in the program mentioned above. The condition was stated in the if statement. Is the number provided less than 40 or not? The if clause assessed the true, carried out the if body, and output the outcome.
Example
void main () {
// define a variable which holds a numeric value
var age = 16;
// if statement check the given condition
if (age>18){
print("You are eligible for voting");
};
print("You are not eligible for voting");
}
Output
You are not eligible for voting
We can observe in the program above that when the if condition evaluated to false, the if body was skipped and the outer statement of the if block was performed instead.