Loops: for and while

on Saturday, 18th of July, 2020

You can repeat expressions in loops using the same keywords as in many languages. There are several kinds of loops in Dart:

  • Standard for
  • for-in
  • while
  • do while

for loops

If you need to know the index, your best bet is the standard for loop:

for (var i = 0; i < 5; i++) {
  print(i);
}

// prints
0
1
2
3
4

If you don't care about the index, the for-in loop is great option. It's easier to read and more concise to write.

List<String> pets = ['Nora', 'Wallace', 'Phoebe'];
for (var pet in pets) {
  print(pet);
}

// prints
'Nora'
'Wallace'
'Phoebe'

while loops

while loops behave as you'd expect. They evaluate the condition before the loop runs -- meaning it may never run at all:

while(someConditionIsTrue) {
  // do some things
}

do-while loops, on the other hand, evaluate the condition after the loop runs. So they always execute the code in the block at least once:

do {
  // do somethings at least once
} while(someConditionIsTrue);

break and continue

These two keywords help you manipulate the flow of the loop. Use continue in a loop to immediately jump to the next iteration, and use break to break out of the loop completely:

for (var i = 0; i < 55; i++) {
  if (i == 5) {
    continue; // jump to next iteration
  }
  if (i == 10) {
    break; // stop loop immediately
  }
  print(i);
}

// prints to the console
0
1
2
3
4
6
7
8
9


Join thousands of Flutter developers.

Sign up for infrequent updates about Flutter and Dart.

You can get all this content and more in one place. Check out my new book Flutter in Action