Open In App
Related Articles

Dart – Break Statement

Improve Article
Improve
Save Article
Save
Like Article
Like

The break statement in Dart inside any loop gives you a way to break or terminate the execution of the loop containing it, and hence transfers the execution to the next statement following the loop. It is always used with the if-else construct.

Dart Break Statement Flow Diagram

While Loop:

break statement in dart

 

Do While Loop:

break statement in do-while loop dart

For Loop:

break statement in for loop dart

Syntax: break;

Example 1:

Dart




// Dart program in Dart to 
// illustrate Break statement 
void main()
{
  var count = 0;
  print("GeeksforGeeks - Break Statement in Dart");
    
  while(count<=10)
  {
    count++;
    if(count == 5)
    {
      //break statement
      break
    }
      
    print("Inside loop ${count}");
  }
  print("Out of while Loop");
}


Output:

In this above program, the variable count is initialized as 0. Then a while loop is executed as long as the variable count is less than 10.

Inside the while loop, the count variable is incremented by 1 with each iteration (count = count + 1). Next, we have an If statement that checks the variable count is equal to 5, if it returns TRUE causes loop to break or terminate, Within the loop, there is a cout statement that will execute with each iteration of the while loop until the loop breaks. Then, there is a final cout statement outside of the while loop.

Example 2:

Dart




// Dart program in Dart to 
// illustrate Break statement 
void main() { 
   var i = 1; 
   while(i<=10) { 
      if (i %  == 0) { 
         print("The first multiple of 2  between 1 and 10 is : ${i}"); 
         break ;    
         // exit the loop if the 
         // first multiple is found 
      
      i++; 
   }
}


Output:

The first multiple of 5 between 1 and 10 is: 2 

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 15 Jul, 2020
Like Article
Save Article
Previous
Next
Similar Reads