Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

dart.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.

Learn more
Loops

Dart 3.10 is taking off with dot shorthands, stable build hooks, nuanced deprecation annotations, and more!Learn more

Loops

This page shows how you can control the flow of your Dart code using loops and supporting statements:

  • for loops
  • while anddo while loops
  • break andcontinue

You can also manipulate control flow in Dart using:

For loops

#

You can iterate with the standardfor loop. For example:

dart
varmessage=StringBuffer('Dart is fun');for(vari=0;i<5;i++){message.write('!');}

Closures inside of Dart'sfor loops capture thevalue of the index. This avoids a common pitfall found in JavaScript. For example, consider:

dart
varcallbacks=[];for(vari=0;i<2;i++){callbacks.add(()=>print(i));}for(finalcincallbacks){c();}

The output is0 and then1, as expected. In contrast, the example would print2 and then2 in JavaScript.

Sometimes you might not need to know the current iteration counter when iterating over anIterable type, likeList orSet. In that case, use thefor-in loop for cleaner code:

dart
for(varcandidateincandidates){candidate.interview();}

In the previous example loop,candidate is defined within the loop body and set to reference one value fromcandidates at a time.candidate is a localvariable. Reassigningcandidate inside the loop body only changes the local variable for that iteration and doesn't modify the originalcandidates iterable.

To process the values obtained from the iterable, you can also use apattern in afor-in loop:

dart
for(finalCandidate(:name,:yearsExperience)incandidates){print('$name has$yearsExperience of experience.');}
Tip

Iterable classes also have aforEach() method as another option:

dart
varcollection=[1,2,3];collection.forEach(print);// 1 2 3

While and do-while

#

Awhile loop evaluates the condition before the loop:

dart
while(!isDone()){doSomething();}

Ado-while loop evaluates the conditionafter the loop:

dart
do{printLine();}while(!atEndOfPage());

Break and continue

#

Usebreak to stop looping:

dart
while(true){if(shutDownRequested())break;processIncomingRequests();}

Usecontinue to skip to the next loop iteration:

dart
for(inti=0;i<candidates.length;i++){varcandidate=candidates[i];if(candidate.yearsExperience<5){continue;}candidate.interview();}

If you're using anIterable such as a list or set, how you write the previous example might differ:

dart
candidates.where((c)=>c.yearsExperience>=5).forEach((c)=>c.interview());

Labels

#

A label is an identifier followed by a colon (labelName:) that you can place before a statement to create alabeled statement. Loops and switch cases are often used as labeled statements. A labeled statement can be referenced later in abreak orcontinue statement as follows:

  • break labelName; Terminates the execution of the labeled statement. This is useful for breaking out of a specific outer loop when you're within a nested loop.

  • continue labelName; Skips the rest of the current iteration of the labeled statement loop and continues with the next iteration.

Labels are used to manage control flow. They are often used with loops and switch cases and allow you to specify which statement to break out of or continue, rather than affecting the innermost loop by default.

Labels in for loop usingbreak

#

The following code demonstrates the usage of a label calledouterLoop in afor loop with abreak statement:

dart
outerLoop:for(vari=1;i<=3;i++){for(varj=1;j<=3;j++){print('i =$i, j =$j');if(i==2&&j==2){breakouterLoop;}}}print('outerLoop exited');

In the previous example, wheni == 2 andj == 2, thebreak outerLoop; statement stops both inner and outer loops. As a result, the output is:

i = 1, j = 1i = 1, j = 2i = 1, j = 3i = 2, j = 1i = 2, j = 2outerLoop exited

Labels in for loop usingcontinue

#

The following code demonstrates the use of a label calledouterLoop in afor loop with acontinue statement:

dart
outerLoop:for(vari=1;i<=3;i++){for(varj=1;j<=3;j++){if(i==2&&j==2){continueouterLoop;}print('i =$i, j =$j');}}

In the previous example, wheni == 2 andj == 2,continue outerLoop; skips the rest of the iterations fori = 2 and moves toi = 3. As a result, the output is:

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

Labels in while loop usingbreak

#

The following code demonstrates the use of a label calledouterLoop in awhile loop with abreak statement:

dart
vari=1;outerLoop:while(i<=3){varj=1;while(j<=3){print('i =$i, j =$j');if(i==2&&j==2){breakouterLoop;}j++;}i++;}print('outerLoop exited');

In the previous example, the program breaks out of both inner and outerwhile loops wheni == 2 andj == 2. As a result, the output is:

i = 1, j = 1i = 1, j = 2i = 1, j = 3i = 2, j = 1i = 2, j = 2outerLoop exited

Labels in while loop usingcontinue

#

The following code demonstrates the use of a label calledouterLoop in awhile loop with acontinue statement:

dart
vari=1;outerLoop:while(i<=3){varj=1;while(j<=3){if(i==2&&j==2){i++;continueouterLoop;}print('i =$i, j =$j');j++;}i++;}

In the previous example, the iteration fori = 2 andj = 2 is skipped and the loop moves directly toi = 3. As a result, the output is:

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

Labels in do-while loop usingbreak

#

The following code demonstrates the use of a label calledouterLoop in ado while loop with abreak statement:

dart
vari=1;outerLoop:do{varj=1;do{print('i =$i, j =$j');if(i==2&&j==2){breakouterLoop;}j++;}while(j<=3);i++;}while(i<=3);print('outerLoop exited');

In the previous example, the program breaks out of both inner and outer loops wheni == 2 andj == 2. As a result, the output is:

i = 1, j = 1i = 1, j = 2i = 1, j = 3i = 2, j = 1i = 2, j = 2outerLoop exited

Labels in do-while loop usingcontinue

#

The following code demonstrates the use of a label calledouterLoop in ado while loop with acontinue statement:

dart
vari=1;outerLoop:do{varj=1;do{if(i==2&&j==2){i++;continueouterLoop;}print('i =$i, j =$j');j++;}while(j<=3);i++;}while(i<=3);

In the previous example, the loop skipsi = 2 andj = 2 and moves directly toi = 3. As a result, the output is:

i = 1, j = 1i = 1, j = 2i = 1, j = 3i = 2, j = 1i = 3, j = 1i = 3, j = 2i = 3, j = 3
Was this page's content helpful?

Unless stated otherwise, the documentation on this site reflects Dart 3.10.0. Page last updated on 2025-5-5.View source orreport an issue.


[8]ページ先頭

©2009-2025 Movatter.jp