Dart If Else Statement

In Dart, if-else statement is used to execute one of the two blocks: if-block or else-block; based on the result of a given condition.

In this tutorial, we will learn the syntax and usage of Dart If-Else statement.

Dart If-Else

Dart If-Else statement contains two blocks: if-block and else-block.

If the boolean_expression next to if keyword evaluates to true, then the code inside if-block is executed.

If the boolean_expression next to if keyword evaluates to false, then the code inside else-block is executed.

Syntax

The syntax of if-else statement in Dart is given in the following.

</>
Copy
 if (boolean_expression) {
     //if block statement(s)
 } else {
     //else block statement(s)
 }

where boolean_expression evaluates to or promotes to boolean value true or false.

Example

In the following program, we take an integer value in x, and check if the number in x is even or odd using if-else statement.

main.dart

</>
Copy
void main(){	
	int x = 13;
	
	if(x%2==0){
		print('$x is even number.');
	} else{
		print('$x is odd number.');
	}
}

Output

13 is odd number.

Conclusion

In this Dart Tutorial, we learned the syntax and usage of Dart Conditional Statement: If-Else.