Open In App

Labels in Dart

Last Updated : 28 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Most of the people, who have programmed in C programming language, are aware of goto and label statements which are used to jump from one point to other but unlike Java, Dart also doesn’t have any goto statements but indeed it has labels which can be used with continue and break statements and help them to take a bigger leap in the code.
It must be noted that line-breaks are not allowed between ‘label-name’ and loop control statements.
Example #1: Using label with the break statement 
 

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 above code results into only one-time printing of statement because once the loop is broken it doesn’t go back into it. 
 
Example #2: Using label with the continue statement 
 

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 of the statement twice because of the condition it didn’t break out of the loop and thus printing it twice.
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads