Table of contents
1.
Introduction
2.
Decision Making Statements
2.1.
If Statement
2.2.
if-else Statement
2.3.
else-if ladder
2.4.
Switch Case Statement
3.
FAQs
4.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Dart Decision Making Statements

Author Pradeep Kumar
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will look into the Dart Decision Statements. We will go through all the decision statements supported by Dart. If you are new to Dart and are yet to set up your environment for the language, you can check out this article.

Decision Making Statements

Decision-making statements allow programmers to choose which statement should be executed under different circumstances. The most typical decision-making statements are as follows:

If Statement

It checks whether or not the given statement is true. The code enclosed within the if statement is executed only if the if statement evaluates to true. Otherwise, it's simply ignored.

Syntax:

if(condition){
  //body of the if statement
}

Example:

void main() {
  int num = 87;

  // here the if statement checks if the number is more than 35
  if (num > 35) {
    // execute this print statement if the condition evaluates to true
    print("Pass");
  }
}

Output:

Pass

if-else Statement

If the condition inside the if statement evaluates to true, the piece of code enclosed within it is executed. Otherwise, the code contained within the else statement is run.

Syntax:

if(condition){
  //body of the if statement
}
else{
  //body of the else statement
}

Example:

void main() {
  int num = 32;

  // here the if statement checks if number are less than 35 or not
  if (num < 35) {
    // execute this print statement if the condition evaluates to true
    print("Fail");
  } else {
    // if the condition evaluates to false, execute this print statement.
    print("Pass");
  }
}

Output:

Fail

else-if ladder

The function of an else if statement is similar to an if statement. It is used to check for other conditions after an if statement. If the condition evaluates to true, the code inside it is executed; otherwise, it checks for other else if conditions. 

Syntax:

if(condition_1){
  //body of the if statement
}
else if(condition_2){
  //body of the else if condition
}
.
.
.
else {
  //body of the else statement
}

Example:

void main() {
  int num = 67;

  // here the if statement checks if the number is less than 35
  if (num < 35) {
    // execute this print statement if the condition evaluates to true
    print("Grade: F");
  } else if (num < 45) {
    // print Grade D if the number falls between the range [35,45)
    print("Grade: D");
  } else if (num < 65) {
    // print Grade C if the number falls between the range [45,65)
    print("Grade: C");
  } else if (num < 75) {
    // print Grade B if the number falls between the range [65,75)
    print("Grade: B");
  } else {
    //else print the grade as A
    print("Grade: A");
  }
}

Output:

Grade: B

Switch Case Statement

It works like a long statement containing one if statement and multiple else if statements. The condition inside the switch statement is evaluated, and the value obtained is matched with all the cases. If it matches a case, the code inside that case statement is executed. Otherwise, the code of the default statement is executed.

Syntax:

switch(condition){
  case constant_value1:{
    //body of the case statement
  }
  break;
 
  case constant_value2:{
    //body of the case statement
  }
  break;

  default: {
    // body of the default statement
  }
  break;
}
Example:
void main() {
  String operator = "/";

  // switch statement to check for valid arithmetic operators
  switch (operator) {
    case "+":
      {
        print("Addition operator");
      }
      break;
    case "-":
      {
        print("Subtraction Operator");
      }
      break;
    case "*":
      {
        print("Multiplication Operator");
      }
      break;
    case "/":
      {
        print("Divide Operator");
      }
      break;
    case "//":
      {
        print("Division Operator");
      }
      break;
    case "%":
      {
        print("Modulus Operator");
      }
      break;
    default:
      {
        print("Not a valid arithmetic operator");
      }
      break;
  }
}

Output:

Divide Operator

FAQs

  1. Can you have multiple if statements in Dart?
    Yes, Dart supports nested if statements. Dart allows you to nest any number of if statements inside of each other.
  2. Can you have more than one else-if statement?
    You can have multiple else-if statements preceded by an if statement in your Dart program. 
  3. Are if-else statements faster than switch statements?
    The switch statement allows you to easily redirect execution to different areas of code based on the value of an expression. As a result, switch statements are faster than if-else statements.

Conclusion 

In this article, we have extensively discussed the Decision Making Statements in Dart. We hope that this blog has helped you enhance your knowledge regarding the Decision Statement, and to learn more, check out our article on Dart Classes
And to learn in-depth about android development, check out our Android Development course on the Coding Ninjas website. Do upvote our blog to help other ninjas grow. 
Happy Coding!

Live masterclass