Open In App

Dart - Loops

Last Updated : 26 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A looping statement in Dart or any other programming language is used to repeat a particular set of commands until certain conditions are not completed. There are different ways to do so. They are: 

  • for loop
  • for... in loop
  • for each loop
  • while loop
  • do-while loop

for loop

The for loop in Dart is a powerful construct that closely mirrors the familiar for loop of Java, making it accessible to many developers. This loop is an essential tool for executing a block of code multiple times, and it’s particularly useful when you know in advance how many iterations you want to perform.

Syntax: 

for(initialization; condition; text expression){
// Body of the loop
}

Control flow

The control flow goes as: 

  1. Initialization : This is where you set up your loop. Think of it as the starting line—a place where you declare any variables needed to control the loop, which will run just once at the beginning.
  2. Condition : Here, the loop checks if it should keep running. Each time the loop completes an iteration, it evaluates the condition to see if it still holds. If it does, the loop continues; if not, it stops.
  3. Body of loop : This is the heart of the loop. It contains the code that will execute repeatedly as long as the condition remains true. It’s where the actual work gets done, whether processing data, updating a value, or any repeated task you need to perform.
  4. Increment/Update expression : After the body has executed, this expression is checked again to evaluate the condition for the next cycle. It’s a crucial check that determines whether the loop will run once more or end.

Example: 

Dart
// Printing GeeksForGeeks 5 times
void main()
{
    for (int i = 0; i < 5; i++) {
        print('GeeksForGeeks');
    }
}


Output:

GeeksForGeeks
GeeksForGeeks
GeeksForGeeks
GeeksForGeeks
GeeksForGeeks


for...in the loop

The for...in the loop is a delightful feature in Dart that elegantly lets you iterate through the elements of a collection, such as lists or sets. It simplifies the process of accessing each element, allowing you to focus on what you want to do with the elements rather than the mechanics of looping.

Syntax: 

 for (var in expression) {
// Body of loop
}

Example: 

Dart
void main()
{
    var GeeksForGeeks = [ 1, 2, 3, 4, 5 ];
    for (int i in GeeksForGeeks) {
        print(i);
    }
}


Output: 

1
2
3
4
5


for each ... loop

The forEach loop is a handy way to navigate seamlessly through every single element in a collection.

Syntax:

collection.forEach((value) {
// Body of loop
});

Parameters:

  • ( value){} : This is a function that you create to define what you want to do with each item in the collection. It’s like giving a specific direction or task for each exhibit-whether it’s to look closely at it, explain it, or even modify it.
Dart
void main() {
      var GeeksForGeeks = [1,2,3,4,5];
      GeeksForGeeks.forEach((var num)=> print(num)); 
}


Output:

1
2
3
4
5


while loop

The while loop keeps executing its block of code as long as the condition remains true. This loop is useful in scenarios where the number of times you need to repeat an action isn’t predetermined—perhaps you’re waiting for user input or monitoring a changing variable.

Syntax: 

while (condition) {
// Body of loop
}

Example: 

Dart
void main()
{
    var GeeksForGeeks = 4;
    int i = 1;
    while (i <= GeeksForGeeks) {
        print('Hello Geek');
        i++;
    }
}


Output: 

Hello Geek
Hello Geek
Hello Geek
Hello Geek


do..while loop

The do...while ensures that its block of code runs at least once before it even bothers checking the condition. Imagine committing to try something out first, and only after giving it a shot do you evaluate whether or not you want to continue. This makes the do...while loop particularly useful in situations where you want to execute some initial setup or action before validating any subsequent conditions. 

Syntax: 

 do{
text expression;
// Body of loop
}while(condition);

Example: 

Dart
void main()
{
    var GeeksForGeeks = 4;
    int i = 1;
    do {
        print('Hello Geek');
        i++;
    } while (i <= GeeksForGeeks);
}


Output: 

Hello Geek
Hello Geek
Hello Geek
Hello Geek

In Dart, looping statements allow you to execute a block of code multiple times, either for a fixed number of iterations or based on a condition. Each type of loop serves a distinct purpose:

  • for loop: Ideal when the number of iterations is known in advance.
  • for...in loop: Simplifies the process of iterating over collections, such as lists or sets.
  • forEach loop: Offers a functional approach to iterate over collections.
  • while loop: Continues executing as long as a specified condition remains true, which is useful when the number of iterations is uncertain.
  • do...while loop: Guarantees that the block of code executes at least once, regardless of the condition.

Selecting the appropriate loop depends on the specific problem, the data structure being used, and the requirements for control flow. 


Next Article
Article Tags :

Similar Reads