Ad space (banner)
Lesson 6 / 20

Repeating (the for loop)

This lesson covers looping in Dart with a for statement.

Writing for (init; condition; update) { ... } repeats a block a fixed number of times.

The sample code writes for (int i = 1; i <= 5; i++) to print the numbers 1 through 5 in order.

A common early stumble is an off-by-one count caused by how the condition is written. Keep the difference between i <= 5 and i < 5 in mind so you get exactly the number of repetitions you meant.

In professional work, for loops are used constantly to process every element of a list, and the for-in form covered in the next lesson is just as common.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Dart directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)