Dart for loop

Summary: in this tutorial, you’ll learn how to use the Dart for loop statement to execute statements a fixed number of times.

Introduction to the Dart for loop statement

Dart for statement executes statements a fixed number of times. Here’s the syntax of the for statement:

for(initializer; condition; iterator)
{
    // statement
}Code language: Dart (dart)

The for statement has expressions initializer, condition, iterator, and a loop body.

initializer

The for loop statement executes the initializer only once before entering the first iteration of the loop. Typically, you declare and initialize a loop variable in the initializer.

The variable that you declare in the initializer cannot be accessible outside of the for statement.

condition

The condition is a Boolean expression. The for statement evaluates the condition before each iteration.

If the condition is true, the for loop executes the next iteration. Otherwise, the loop will stop.

iterator

The for loop statement executes the iterator after each iteration.

loop body

The loop body consists of one or more statements surrounded by curly braces. If the condition is true, the for statement executes the loop body in each iteration.

All expressions including initializer, condition, and iterator are optional. Hence, you can have an indefinite for loop like this:

for(;;) 
{
    // statement
}Code language: Dart (dart)

In this case, you need to use the break statement to terminate the loop.

The following flowchart shows how the for loop statement works:

Dart for loop

Dart for loop examples

Let’s take some examples of using the for loop statement.

1) A simple Dart for loop example

The following example uses the for statement to display three numbers 0, 1, and 2:

void main() {
  for (var i = 0; i < 3; i++) {
    print(i);
  }
}Code language: Dart (dart)

Output:

0
1
2
Code language: Dart (dart)

How it works.

In the initializer, declare the variable i and initialize its value to zero:

int i = 0Code language: Dart (dart)

In this condition, check if the variable i is less than 3:

i < 3Code language: Dart (dart)

Since the variable i is zero, which is less than three, the for statement executes its loop body that displays the variable i:

print(i)Code language: Dart (dart)

After the first iteration, the for statement executes the iterator that increases the value of i by one:

i++Code language: Dart (dart)

The i variable is 1.

Starting from the second iteration, the for statement only evaluates the condition and doesn’t execute the initializer again. The condition determines whether the for loop should continue the next iteration.

i < 3Code language: Dart (dart)

Because the variable i is one, the expression evaluates to true. Hence, the for statement runs its loop body the second time that displays the variable i and executes the iterator that increases the variable i by one:

i++Code language: Dart (dart)

After this step, the variable i is two. The for loop continues checking the condition to determine if it should continue to the next iteration:

i < 3Code language: Dart (dart)

The condition is true because the variable i is two. Therefore, the for statement runs the third iteration that displays i and increases the value of i by one.

This time the i variable is three making the condition false. Hence, the loop exits.

In short, the for loop runs its body three times that displays three numbers 0, 1, and 2.

2) Using the Dart for loop to calculate the total of integers

The following example uses a for loop to calculate the total of integers from 1 to 10:

void main() {
  int total = 0;
  for (var i = 1; i <= 10; i++) {
    total += i;
  }
  print(total);
}
Code language: Dart (dart)

Output:

55Code language: Dart (dart)

In this example, the for statement adds up the variable i to the total in each iteration from 1 to 10.

3) Using the Dart for loop to display even numbers in the range

The following example uses the for loop statement to display even numbers between 0 and 10:

void main() {
  for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
      print(i);
    }
  }
}Code language: Dart (dart)

Output:

0
2
4
6
8Code language: Dart (dart)

In this example, the for loop displays only even numbers between 0 and 9.

4) Using the Dart for loop to display every character of a string

The following example uses the for statement to display every character of a string:

void main() {
  String language = "Dart";

  for (int i = 0; i < language.length; i++) {
    print(language[i]);
  }
}
Code language: Dart (dart)

Output:

D
a
r
tCode language: Dart (dart)

In this example, the string language has an index that starts from 0 and ends at message.length - 1. And the program uses a for loop statement to iterate over the characters using an index.

Summary

  • Use the Dart for loop to execute statements repeatedly a fixed number of times.
Was this tutorial helpful ?