Dart For Loop

Repeating code with precision and control

🔁 What is For Loop?

For loops repeat code a specific number of times. They're perfect when you know exactly how many iterations you need, like counting from 1 to 10 or processing items in a list.


// Simple for loop example
void main() {
  for (int i = 1; i <= 5; i++) {
    print('Count: $i');
  }
  print('Loop finished!');
}
                                    

Types of For Loops

🔢

Traditional For

Classic loop with counter variable

for (int i = 0; i < 5; i++) {
  print(i);
}
📋

For-in Loop

Iterate through collections

for (String name in names) {
  print(name);
}
🎯

For-each Method

Functional approach to iteration

numbers.forEach((num) {
  print(num);
});
🔄

Nested Loops

Loops inside other loops

for (int i = 0; i < 3; i++) {
  for (int j = 0; j < 3; j++) {
    print('$i,$j');
  }
}

🔹 Basic For Loop

The traditional for loop has three parts: initialization, condition, and increment:

void main() {
  // for (initialization; condition; increment)
  for (int i = 1; i <= 5; i++) {
    print('Number: $i');
  }
}

Output:

Number: 1

Number: 2

Number: 3

Number: 4

Number: 5

🔹 For-in Loop

Iterate through lists, sets, or any iterable collection:

void main() {
  List fruits = ['Apple', 'Banana', 'Orange', 'Mango'];
  
  print('My favorite fruits:');
  for (String fruit in fruits) {
    print('- $fruit');
  }
}

Output:

My favorite fruits:

- Apple

- Banana

- Orange

- Mango

🔹 Loop with Index

Access both index and value when iterating:

void main() {
  List colors = ['Red', 'Green', 'Blue'];
  
  // Method 1: Traditional for loop
  for (int i = 0; i < colors.length; i++) {
    print('${i + 1}. ${colors[i]}');
  }
  
  print('---');
  
  // Method 2: Using asMap()
  colors.asMap().forEach((index, color) {
    print('${index + 1}. $color');
  });
}

Output:

1. Red

2. Green

3. Blue

---

1. Red

2. Green

3. Blue

🔹 Nested For Loops

Use loops inside other loops for multi-dimensional operations:

void main() {
  print('Multiplication Table (1-3):');
  
  for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
      int result = i * j;
      print('$i × $j = $result');
    }
    print('---'); // Separator between tables
  }
}

Output:

Multiplication Table (1-3):

1 × 1 = 1

1 × 2 = 2

1 × 3 = 3

---

2 × 1 = 2

2 × 2 = 4

2 × 3 = 6

---

3 × 1 = 3

3 × 2 = 6

3 × 3 = 9

---

🔹 Loop Control Statements

Control loop execution with break and continue:

void main() {
  print('Numbers 1-10 (skipping 5, stopping at 8):');
  
  for (int i = 1; i <= 10; i++) {
    if (i == 5) {
      continue; // Skip this iteration
    }
    if (i == 8) {
      break; // Exit the loop
    }
    print('Number: $i');
  }
  
  print('Loop ended');
}

Output:

Numbers 1-10 (skipping 5, stopping at 8):

Number: 1

Number: 2

Number: 3

Number: 4

Number: 6

Number: 7

Loop ended

🔹 Practical Examples

Real-world applications of for loops:

🔸 Sum of Numbers

void main() {
  int sum = 0;
  
  for (int i = 1; i <= 100; i++) {
    sum += i;
  }
  
  print('Sum of numbers 1-100: $sum');
}

🔸 Finding Even Numbers

void main() {
  List evenNumbers = [];
  
  for (int i = 1; i <= 20; i++) {
    if (i % 2 == 0) {
      evenNumbers.add(i);
    }
  }
  
  print('Even numbers 1-20: $evenNumbers');
}

🧠 Test Your Knowledge

What are the three parts of a traditional for loop?