Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Break and Continue


Share

Dart Break

The break statement stops a loop at once.

Dart then goes on with the code after the loop.

This example stops the loop when i is equal to 4:

Example

void main() {
  for (var i = 0; i < 10; i++) {
    if (i == 4) {
      break;
    }
    print(i);
  }
  print('The loop is over');
}
Try it Yourself »

Result:

0
1
2
3
The loop is over

A common use of break is to stop searching when you have found what you are looking for:

Example

void main() {
  final fruits = ['apple', 'banana', 'cherry', 'kiwi', 'mango'];
  for (final fruit in fruits) {
    print('Checking $fruit');
    if (fruit == 'cherry') {
      print('Found it!');
      break;
    }
  }
}
Try it Yourself »

Result:

Checking apple
Checking banana
Checking cherry
Found it!

Dart Continue

The continue statement skips the rest of the current run of the loop.

The loop then goes on with the next run.

This example skips the value 4:

Example

void main() {
  for (var i = 0; i < 7; i++) {
    if (i == 4) {
      continue;
    }
    print(i);
  }
}
Try it Yourself »

Result:

0
1
2
3
5
6

This example prints only the odd numbers, by skipping every even number:

Example

void main() {
  for (var i = 1; i <= 9; i++) {
    if (i % 2 == 0) {
      continue;
    }
    print(i);
  }
}
Try it Yourself »

Result:

1
3
5
7
9


Break and Continue in While Loops

You can also use break and continue in while loops.

Here break stops a loop whose condition is always true:

Example

void main() {
  var tries = 0;
  while (true) {
    tries++;
    print('Try number $tries');
    if (tries == 3) {
      break;
    }
  }
  print('Stopped after $tries tries');
}
Try it Yourself »

Result:

Try number 1
Try number 2
Try number 3
Stopped after 3 tries

When you use continue in a while loop, change the counter before continue.

If you do not, the counter never changes, and the loop never ends:

Example

void main() {
  var i = 0;
  while (i < 6) {
    i++;
    if (i == 3) {
      continue;
    }
    print(i);
  }
}
Try it Yourself »

Result:

1
2
4
5
6

Break in Nested Loops

In a nested loop, break only stops the inner loop.

The outer loop goes on:

Example

void main() {
  for (var i = 1; i <= 3; i++) {
    for (var j = 1; j <= 3; j++) {
      if (j == 2) {
        break;
      }
      print('i = $i, j = $j');
    }
  }
}
Try it Yourself »

Result:

i = 1, j = 1
i = 2, j = 1
i = 3, j = 1

To stop the outer loop from inside the inner loop, give the outer loop a label.

A label is a name followed by a colon :, written just before the loop.

Then write break with the label name:

Example

void main() {
  outer:
  for (var i = 1; i <= 3; i++) {
    for (var j = 1; j <= 3; j++) {
      if (j == 2) {
        break outer;
      }
      print('i = $i, j = $j');
    }
  }
  print('Both loops are over');
}
Try it Yourself »

Result:

i = 1, j = 1
Both loops are over


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.