Table of contents
1.
Introduction
2.
Dart Fallthrough
2.1.
Skipping Break Statement
2.2.
Empty Case statement
2.3.
Replace break with a continue statement
3.
Frequently Asked Questions
3.1.
Is it mandatory to add a break statement in case there is only one case statement?
3.2.
Is there any restriction on the name of the label that is to be used after the continue statement?
3.3.
What happens if you miss the break statement in the default case?
4.
Conclusion 
Last Updated: Mar 27, 2024

Dart Fallthrough

Author Pradeep Kumar
2 upvotes
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 Fallthrough. Dart Fallthrough is an error that is generated in switch-case statements. We will discuss how and when this error is generated. 

If you are new to Dart and are yet to set up your environment for the language, you can check out this article.

Dart Fallthrough

We get a Fallthrough error when we forget to include a break statement in our switch-case statements. The control flow shifts to the following line in that instance. If no break appears, control will flow through all cases that follow the true case until the break or the switch's end is reached.

Skipping Break Statement

The most obvious way of creating a Fallthrough error is by skipping the break statements. This method works in many languages except Dart. Dart will throw a compilation error in such a case. For example,

// Main function to run the program
void main() {
  String operator = "+";

  // switch-case statements
  switch (operator) {
    case "+": // Addition Operator
      {
        print("This is the Addition operator.");
      }
    case "-": // Subtraction Operator
      {
        print("This is the Subtraction operator.");
      }
    case "*": // Multiplication Operator
      {
        print("This is the Multiplication operator.");
      }
    case "/": // Division Operator
      {
        print("This is the Division operator.");
      }
    default: // Default case
      {
        print("This is the default case.");
      }
  }
}


Output:

main.dart:7:5: Error: Switch case may fall through to the next case.
    case "+": // Addition Operator
    ^
main.dart:11:5: Error: Switch case may fall through to the next case.
    case "-": // Subtraction Operator
    ^
main.dart:15:5: Error: Switch case may fall through to the next case.
    case "*": // Multiplication Operator
    ^
main.dart:19:5: Error: Switch case may fall through to the next case.
    case "/": // Division Operator

Empty Case statement

Dart supports empty case statement. Empty cases will fall through to the next statement. For example,

// Main function to run the program
void main() {
  String operator = "*";

  // switch-case statements
  switch (operator) {
    case "+": // Addition Operator
      {
        print("This is the Addition operator.");
        break;
      }
    case "-": // Subtraction Operator
      {
        print("This is the Subtraction operator.");
        break;
      }
    case "*": // Multiplication Operator // Empty case
    case "/": // Division Operator
      {
        print("This is the Division operator.");
        break;
      }
    default: // Default case
      {
        print("This is the default case.");
      }
  }
}


Output:

This is the Division operator.

As you can see in the above example, when the condition reaches the empty case statement, it will Fallthrough and execute the following case statement. In this case, as the case statement for "*" was empty, it executed the print statement for the next case(i.e., "/").

Replace break with a continue statement

One other way of achieving Fallthrough in Dart is to use a continue instead of a break statement. For example,

// Main function to run the program
void main() {
  String operator = "-";

  // switch-case statements
  switch (operator) {
    case "+": // Addition Operator
      {
        print("This is the Addition operator.");
        break;
      }
    case "-": // Subtraction Operator
      {
        print("This is the Subtraction operator.");
        continue nextStatement;  // continue to the next statement
      }
    nextStatement: case "*": // Multiplication Operator
      {
        print("This is the Multiplication operator.");
        break;
      }
    case "/": // Division Operator
      {
        print("This is the Division operator.");
        break;
      }
    default: // Default case
      {
        print("This is the default case.");
        break;
      }
  }
}


Output:

This is the Subtraction operator.
This is the Multiplication operator.

Frequently Asked Questions

Is it mandatory to add a break statement in case there is only one case statement?

In case there is only one case statement in a switch-case statement, Dart allows the break statement to be skipped.

 

Is there any restriction on the name of the label that is to be used after the continue statement?

The name of the label can be anything except the word "continue". As continue is a keyword, therefore using it as a label name will through an error.

 

What happens if you miss the break statement in the default case?

Skipping the break statement in the default case will not throw an error as long as the other part of the switch-case statements is correct. 

Conclusion 

In this article, we have extensively discussed Fallthrough in Dart. We hope that this blog has helped you enhance your knowledge regarding the Dart Fallthrough, and to learn more, check out our other article on Dart Operators.

And also, check out the awesome content on the Coding Ninjas Website,
Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass