Open In App

Labels in Dart

Last Updated : 26 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Most people, who have programmed in C programming language, are aware of goto and label statements which are used to jump from one point to another unlike Java, Dart also doesn't have any goto statements but indeed it has labels that can be used with continue and break statements and help them to take a bigger leap in the code.
Dart does allow line breaks between labels and loop control statements.

Using a label with the break statement 

Example :

Dart
void main() {  

  // Defining the label
  Geek1:for(int i=0; i<3; i++)
  {
    if(i < 2)
    {
      print("You are inside the loop Geek");

      // breaking with label
      break Geek1;
    }
    print("You are still inside the loop");
  }
}


Output: 

You are inside the loop Geek

The loop does not return because a break with a label exits the entire labeled loop rather than just the inner loop.


Using a label with the continue statement  

Example :

Dart
void main() {  

  // Defining the label
  Geek1:for(int i=0; i<3; i++)
  {
    if(i < 2)
    {
      print("You are inside the loop Geek");

      // Continue with label
      continue Geek1;
    }
    print("You are still inside the loop");
  }
}


Output: 

You are inside the loop Geek
You are inside the loop Geek
You are still inside the loop

The above code results in printing the statement twice because of the condition; it didn't break out of the loop and thus printed it twice.

Dart does not have a goto statement like C, but it does support labels for use with break and continue. These labels enable control to be transferred to specific loops, making it easier to exit nested loops or skip iterations efficiently.

  • Using break with a label: When you use break with a label, it exits the entire labeled loop, not just the innermost loop.
  • Using continue with a label: When you use continue with a label, it skips the current iteration of the labeled loop and proceeds to the next iteration.

By understanding how to use labels, you can effectively manage complex loops without unnecessary nesting or redundant conditions. 


Next Article
Article Tags :

Similar Reads