## Dart If Else Statements

In Dart, if statement, if-else statement, and if-else-if statement are used to execution a block of code based on a given condition.

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

## 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 values `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 this example, we will try to conditionally execute a block of statements based on a boolean expression.

main.dart

```void main(){

int a = 10;

if(a%2==0){
print('\$a is even number.');
}

if(a%2==1){
print('\$a is odd number.');
}
}```

Output

`10 is even number.`

From the above dart program:

• In the first if statement, the boolean expression evaluates to true, and hence the statements inside that if block are executed.
• In the second if statement, the boolean expression evaluates to false, and hence the statements inside that if block are not executed.

## 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, the code inside if block is executed and further execution continues with the statements next to if-else block.

If the boolean_expression next to if keyword evaluates to false, the code inside else block is executed and further execution continues with the statements next to if-else block.

### Syntax

The syntax of if statement in Dart is shown below.

```if (boolean_expression) {
//if block statement(s)
} else {
//else block statement(s)
}```

where the `boolean_expression` evaluates to or promotes to boolean values `true` or `false`.

If the boolean_expression is true, if block statement(s) get executed.

If the boolean_expression is false, else block statement(s) get executed.

### Example

In this example, we will try to check if the given number is even or odd using if-else block.

main.dart

```void main(){

int a = 13;

if(a%2==0){
print('\$a is even number.');
} else{
print('\$a is odd number.');
}
}```

Output

`13 is odd number.`

## Dart If-Else-If

Dart If-Else-If statement is and extension to if-else statement. If-Else-If contains more than one boolean expression.

### Syntax

The syntax of if statement in Dart is shown below.

```if (boolean_expression_1) {
//statement(s)
} else if (boolean_expression_2) {
//statement(s)
} else {
//else block statement(s)
}```

If-Else-If ladder can contain multiple else-if blocks, but only one mandatory if block at the start and an optional else block at the end of ladder.

The execution checks the boolean expressions sequentially one by one. While evaluation if the boolean expression evaluates to true, the corresponding block of statements are executed, else, the program control goes with the evaluation of next boolean expression in the ladder. If none of the boolean expressions evaluate to true, else block is executed.

### Example

In this example, we will try to check if the given number is negative, zero or positive using if-else-if block.

main.dart

```import 'dart:io';

void main(){
print('Enter a number');
int a = int.parse(line);

if(a<0){
print('\$a is negative number.');
} else if(a==0) {
print('\$a is zero. Neither negative nor positive');
} else {
print('\$a is positive number.');
}
}```

Output

### Conclusion

In this Dart Tutorial, we learned about Dart Conditional Statements: If, If-Else and If-Else-If ladder.