Learn/Dart/5. Loops
Lesson 5 of 8
Sign in

Dart, lesson 5

Loops

A loop runs the same block of code many times. Instead of writing print ten times, you write it once and tell Dart how often to repeat it.

The counting for loop

The classic for loop has three parts in its parentheses, separated by semicolons:

  1. var i = 1 runs once, before the loop starts.
  2. i <= 5 is checked before every round. When it's false, the loop ends.
  3. i++ runs after every round.

So this prints Lap 1 to Lap 5. The variable i only exists inside the loop. To count down, start high and use i--; to count in steps of two, use .

2 exercises to try what you just read.

Exercise 1

Times table

Press Ctrl Enter or Check to test your code.

Exercise 2

Step counter

Press Ctrl Enter or Check to test your code.

i += 2

Looping over items: for-in

When you already have a collection of things, for-in hands you each one in turn, with no index to manage:

Use final for the loop variable unless you plan to change it inside the body. You'll see much more of lists in the collections lesson.

while loops

A while loop repeats as long as its condition stays true. Use it when you don't know in advance how many rounds you'll need:

Make sure something inside the loop eventually makes the condition false, or the program runs forever. A do { ... } while (condition); loop is the same idea, except the body always runs at least once because the check happens at the end.

Reading until input runs out

while pairs nicely with stdin.readLineSync(), which returns null once there are no more lines. Read, check, and stop:

Leaving early: break and continue

  • break jumps out of the loop immediately.
  • continue skips the rest of this round and moves to the next one.

break is perfect for searches: once you've found what you want, there is no reason to keep looking.

Further reading

  • Loops
  • Stdin.readLineSync API

Read a number n and print its times table from 1 to 10, one line each, in the form n x i = result.

Input:

Output:

Read daily step counts, one per line, until the input runs out or you reach a line with 0 (a broken tracker, which also ends the list and is not counted). Print how many days were counted and the total number of steps.

Input:

Output:

If there are no counted days, print Days: 0 and Steps: 0.

for (var i = 1; i <= 5; i++) {
  print('Lap $i');
}
final fruits = ['apple', 'kiwi', 'mango'];
for (final fruit in fruits) {
  print('I like $fruit');
}
var balance = 100;
var years = 0;
while (balance < 200) {
  balance = balance * 110 ~/ 100; // grow by 10%, whole euros
  years++;
}
print('Doubled after $years years');
var total = 0;
String? line;
while ((line = stdin.readLineSync()) != null) {
  total += int.parse(line!);
}
for (var n = 1; n <= 10; n++) {
  if (n % 2 == 0) continue; // skip even numbers
  if (n > 7) break;         // stop completely after 7
  print(n);                 // 1, 3, 5, 7
}
7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
4000
8500
0
9999
Days: 2
Steps: 12500