Dart if, if-else, if-else-if Ladder & Nested if-else with Examples

In dart, decision can be made using if else statement. Dart supports if statements with optional else statements.

In this tutorial, we will learn about if statement, if-else statement, if-else-if ladder & nested if-else in dart programming with examples.

Dart if Statement

The syntax of dart if statement


if(test_expression){
	// statements to be executed if test_expression is true	
}

// next statement

Working of if Statement

The if statement first evaluates the test_expression inside the parenthesis () and then does following task:

  1. If test_expression evaluates to true then statements within if block are executed and control goes to next statement.
  2. If test_expression evaluates to false then statements within if block are neglected and control goes to next statement.

if Examples

Example 1


void main() {
  int number = 20;
  
  if(number>18){
    print("Number is $number.");
  }
  
  print("You tested if!");
}

Output

Number is 20.
You tested if!

Here number is set to 20. number > 18 evaluates to true. So statement print("Number is $number."); is executed.


Example 2


void main() {
  int number = 10;
  
  if(number>18){
    print("Number is $number.");
  }
  
  print("You tested if!");
}

Output

You tested if!

Here number is set to 10. number > 18 evaluates to false. So statement print("Number is $number."); is not executed.

Dart if-else Statement

The syntax of dart if else statement


if(test_expression){
	// statements to be executed if test_expression is true
}else{
	// statements to be executed if test_expression is false
}
// next statement

Working of if-else Statement

The if statement first evaluates the test_expression inside the parenthesis () and then does following task:

  1. If test_expression evaluates to true then statements within if block are executed and control goes to next statement.
  2. If test_expression evaluates to false then statements within else block are executed and control goes to next statement.

if-else Examples

Example 1


void main() {
  int age = 20;
  
  if(age>18){
    print("You can vote.");
  }else{
    print("You can not vote.");
  }
}

Output

You can vote.

Here age is set to 20. age > 18 evaluates to true. So statement print("You can vote."); is executed.


Example 2


void main() {
  int age = 15;
  
  if(age>18){
    print("You can vote.");
  }else{
    print("You can not vote.");
  }
}

Output

You can not vote.

Here age is set to 15. age > 18 evaluates to false. So statement print("You can not vote."); is executed.

Dart if-else-if Ladder

if-else-if ladder is quite useful when there are multiple conditions to check.

The syntax of dart if-else-if ladder


if(test_expression_1){
	// statements to be executed if test_expression_1 is true
}else if(test_expression_2){
	// statements to be executed if test_expression_2 is true
}else if(test_expression_3){
	// statements to be executed if test_expression_3 is true
}else{
	// statements to be executed if none of above test_expression are true
}
	
// next statement

Working of if-else-if Ladder

  1. It first evaluates the test_expression_1. If it is true then it executes statements within its block and control goes to next statement otherwise checks another test expression.
  2. Simialrly, It evaluates the test_expression_2. If it is true then it executes statements within its block and control goes to next statement otherwise checks another test expression.
  3. Fianlly, if no test expression evaluates to true the it executes statements within else block and control goes to next statement.

if-else-if Ladder Examples

Example


void main() {
  int number = 100;
  
  if(number==0){
    print("ZERO");
  }else if(number>0){
    print("POSITIVE");
  }else{
    print("NEGATIVE");
  }
}

Output

POSITIVE

Here number is set to 100. number > 0 evaluates to true. So statement print("POSITIVE"); is executed.

Nested if else Statement

In dart, we can have if-else statement nested within another if-else statement.

Nested if-else Example


void main() {
  int number = 40;
  
  if(number>50){
    if(number%10==0){
      print("$number is greater than 50 and divisible by 10");
    }else{
      print("$number is greater than 50 but not divisible by 10");
    }
  }else{
    if(number%10==0){
      print("$number is less than 50 and divisible by 10");
    }else{
      print("$number is less than 50 but not divisible by 10");
    }
  }
  
}

Output

40 is less than 50 and divisible by 10