Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart For Loop


Share

Dart For Loop

When you know how many times a loop must run, use the for loop:

for (statement 1; statement 2; statement 3) {
  // code block to run
}
  • Statement 1 runs one time, before the loop starts
  • Statement 2 is the condition, tested before each run of the loop
  • Statement 3 runs after each run of the loop

This example prints the numbers 0 to 4:

Example

void main() {
  for (var i = 0; i < 5; i++) {
    print(i);
  }
}
Try it Yourself »

Result:

0
1
2
3
4

Example Explained

  • var i = 0 creates the variable i and sets it to 0
  • i < 5 is the condition, so the loop runs as long as i is less than 5
  • i++ adds 1 to i after each run

The variable i only exists inside the loop.

You can change statement 3 to count in other steps.

This example prints only the even numbers from 0 to 10:

Example

void main() {
  for (var i = 0; i <= 10; i += 2) {
    print(i);
  }
}
Try it Yourself »

Result:

0
2
4
6
8
10

This example counts down from 5 to 1:

Example

void main() {
  for (var i = 5; i > 0; i--) {
    print(i);
  }
}
Try it Yourself »

Result:

5
4
3
2
1

A loop can also add up values.

This example adds the numbers 1 to 100:

Example

void main() {
  var sum = 0;
  for (var i = 1; i <= 100; i++) {
    sum += i;
  }
  print('The sum is $sum');
}
Try it Yourself »

Result:

The sum is 5050


Nested Loops

A loop inside another loop is called a nested loop.

The inner loop runs fully for each run of the outer loop:

Example

void main() {
  for (var i = 1; i <= 2; i++) {
    print('Outer: $i');
    for (var j = 1; j <= 3; j++) {
      print('  Inner: $j');
    }
  }
}
Try it Yourself »

Result:

Outer: 1
  Inner: 1
  Inner: 2
  Inner: 3
Outer: 2
  Inner: 1
  Inner: 2
  Inner: 3

The For-In Loop

A list holds many values in one variable, written in square brackets [].

You will learn more about lists in the Dart Lists chapter.

The for-in loop goes through every item in a list, one by one:

for (final item in list) {
  // code block to run
}

Example

void main() {
  final cars = ['Volvo', 'BMW', 'Ford', 'Mazda'];
  for (final car in cars) {
    print(car);
  }
}
Try it Yourself »

Result:

Volvo
BMW
Ford
Mazda

The variable car holds a new item each time, so it can be final.

The for-in loop also works with other collections, like sets.


The forEach() Method

Lists also have a forEach() method.

It runs a function for each item in the list:

Example

void main() {
  final fruits = ['apple', 'banana', 'cherry'];
  fruits.forEach(print);
}
Try it Yourself »

Result:

apple
banana
cherry

Here, print is written without parentheses, so it is not called at once.

Instead, forEach() calls it one time for each item.

You can also write a small function in place, with the item as its parameter:

Example

void main() {
  final fruits = ['apple', 'banana', 'cherry'];
  fruits.forEach((fruit) {
    print('I like $fruit');
  });
}
Try it Yourself »

Result:

I like apple
I like banana
I like cherry

You will learn more about functions in the Dart Functions chapter.

Tip: In most cases, the for-in loop is easier to read than forEach().

The Dart style guide also prefers for-in when you write the function in place.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.