Table of contents
1.
Introduction
2.
Using Labels in Dart
2.1.
Using Labels with the Break Statement
2.2.
Using Labels with the Continue Statement
3.
FAQs
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dart Labels

Author Vivek Goswami
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Labels generally help us to point out something in particular. We can use them for identifying a specific place or location as we go on to mentioning them and using them as references later. In C Programming, we have the label and goto statement. These help us jump in our control from one point to another. Deriving a similar analogy, we can also understand the usage of labels in Dart.

Dart allows us to use labels that let us control the flow of the program more efficiently. A label is primarily an identifier followed by a colon. We can apply it to a statement or a block of code. 

With this basic idea of labels in our minds, let us move on towards learning more about it in detail.  

Using Labels in Dart

As mentioned before, labels help us efficiently determine the program's control flow. Dart doesn't have any goto statement as in C. It uses only the label statement. The label statement allows us to take a more significant leap in the code. 

There are ba two possible scenarios in which we can use labels in Dart:

  1. With the break statement
  2. With the Continue Statement

Let us have a look at the implementation of these methods one by one. 

Using Labels with the Break Statement

The break is a loop control statement used to end a loop. If the compiler finds a break statement inside the loop, the loop iteration ends there, and then control returns immediately from the loop to the first statement after the loop.

The syntax for the break statement is as follows:

Syntax:

break label_name;

We use break statements in two cases usually:

  1. When we don’t have an idea about the exact number of iterations required to perform a particular task
  2. We need to terminate the loop based on a specific condition.

Let us have a look at the implementation of the break statement in the dart code.

Here’s the code:

Code:

void main() {
// Definition the label
  NINJA:
  for (int i = 0; i < 3; i++) {
    if (i < 2) {
      print("You are in the loop, Ninja!");

      // breaking using the label
      break NINJA;
    }
    print("You are still not out of the loop.");
  }
}

Output:

You are in the loop, Ninja!

Using Labels with the Continue Statement

Like the break statement, continue is a loop control statement. The continue statement is the opposite of the break statement.

 It forces the execution of the next iteration of the loop instead of ending the loop. 

As the name implies, the continue statement forces the loop to continue or perform the next iteration. When the continue statement is executed inside the loop, the code inside the loop after the continue statement is skipped, and the next iteration of the loop begins.

Syntax:

continue label_name;

Let us now look at the label's implementation with the continue statement in Dart.

Code:

void main() {
// Definition the label
  NINJA:
  for (int i = 0; i < 3; i++) {
    if (i < 2) {
      print("You are in the loop Ninja!");

      // breaking using the label
      continue NINJA;
    }
    print("You are still not out of the loop");
  }
}

Output:

You are in the loop, Ninja!
You are in the loop, Ninja!
You are still not out of the loop.

FAQs

  1. What are labels?
    A label is primarily an identifier followed by a colon. We can apply it to a statement or a code block to leap the code. Labels help us to determine the flow of the execution of the code.
  2. In what cases are labels generally used in dart programming?
    There are basically two possible scenarios in which we can use labels in Dart:
    With the break statement.
    With the Continue Statement.
  3. What is a break statement?
    The break is a loop control statement used to end a loop. It is used for the termination of a loop. 

Conclusion

In this article, we have extensively discussed Labels in Dart and saw a sample program having its implementation. Feel free to refer to the blog Dart const and Final keyword on the Coding Ninjas Website to enhance your knowledge of Dart programming.

We hope this blog has helped you enhance your knowledge regarding Dart Labels. If you want to learn more, check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development. Do upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass