Labels in Dart

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

filter_none

edit
close

play_arrow

link
brightness_4
code

void main() {  
  
  // Definig 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");
  }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

void main() {  
  
  // Definig 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");
  }
}

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.