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:
- With the break statement
- 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:
- When we don’t have an idea about the exact number of iterations required to perform a particular task
- 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.




