Skip to main content

Command Palette

Search for a command to run...

Understanding Loops in Dart: A Beginner's Guide

Updated
3 min read

Loops are fundamental in programming, enabling you to repeat a block of code multiple times without redundancy. In Dart, loops are powerful and easy to use, making them indispensable for tasks like iterating through data or automating repetitive actions.


Types of Loops in Dart

Dart provides several types of loops:

  1. For Loop

  2. While Loop

  3. Do-While Loop

  4. For-In Loop

  5. For Each Loop

Let’s explore each type with examples.


1. For Loop

The for loop is used when the number of iterations is known beforehand.

Syntax

for (initialization; condition; increment/decrement) {
  // code to be executed
}

Example

void main() {
  for (int i = 1; i <= 5; i++) {
    print('Iteration $i');
  }
}

Output:

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

2. While Loop

The while loop executes a block of code as long as the condition is true.

Syntax

while (condition) {
  // code to be executed
}

Example

void main() {
  int count = 1;
  while (count <= 5) {
    print('Count: $count');
    count++;
  }
}

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

3. Do-While Loop

The do-while loop is similar to the while loop, but it guarantees the execution of the code block at least once.

Syntax

do {
  // code to be executed
} while (condition);

Example

void main() {
  int count = 1;
  do {
    print('Count: $count');
    count++;
  } while (count <= 5);
}

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

4. For-In Loop

The for-in loop is specifically designed to iterate over collections like lists or sets.

Syntax

for (var element in collection) {
  // code to be executed
}

Example

void main() {
  List<String> fruits = ['Apple', 'Banana', 'Cherry'];

  for (var fruit in fruits) {
    print(fruit);
  }
}

Output:

Apple
Banana
Cherry

Loop Control Statements

Dart also provides control statements to manage loop execution:

  1. Break: Exits the loop prematurely.

    Example

     void main() {
       for (int i = 1; i <= 5; i++) {
         if (i == 3) {
           break;
         }
         print('Iteration $i');
       }
     }
    

    Output:

     Iteration 1
     Iteration 2
    
  2. Continue: Skips the current iteration and proceeds to the next.

    Example

     void main() {
       for (int i = 1; i <= 5; i++) {
         if (i == 3) {
           continue;
         }
         print('Iteration $i');
       }
     }
    

    Output:

     Iteration 1
     Iteration 2
     Iteration 4
     Iteration 5
    

Conclusion

Loops are a cornerstone of Dart programming, helping you write efficient and concise code. With the various types of loops and control statements, you can handle any repetitive tasks or data iteration easily. Start practicing loops today to enhance your Dart programming skills!

More from this blog

S

Suneel D.K

25 posts

Learn Flutter and Dart with easy tutorials and YouTube videos! Master app development with tips, tricks, and detailed guides for beginners and pros alike. Start building today!