Loops & Iteration
Repeating Actions Like a Pro
Open interactive version (quiz + challenge)Real-world analogy
Loops are like a merry-go-round at the playground. You keep going around and around until someone says stop. A 'for' loop is like saying 'go around exactly 10 times.' A 'while' loop is like saying 'keep going until the music stops.' Without loops, you would have to write the same code hundreds of times!
What is it?
Loops repeat code multiple times. Dart has for loops (counted repetition), for-in loops (iterate collections), while loops (repeat while condition is true), do-while (at least once), and forEach (functional style). break exits early, continue skips an iteration. Functional methods like map/where/reduce often replace manual loops.
Real-world relevance
Loops are everywhere in apps: displaying a list of messages (loop through each one), processing API response data (loop through results), retrying a network request (while not successful), animating UI elements (loop each frame). Without loops, you would need millions of lines of code!
Key points
- for Loop - Counted Repetition — The for loop repeats code a specific number of times. It has three parts: initialization (start), condition (keep going?), and increment (next step). The classic loop for counting, iterating through indices, and precise control.
- for-in Loop - Collection Iteration — for-in loops through every item in a collection without needing an index. It is cleaner and less error-prone than a regular for loop when you just need each item.
- forEach - Functional Style — forEach() is a method on collections that calls a function for each item. It is similar to for-in but uses a callback function. Some developers prefer it for its concise syntax, especially with arrow functions.
- while Loop - Conditional Repetition — A while loop keeps running as long as its condition is true. Use it when you do not know in advance how many times to loop. Be careful -- if the condition never becomes false, you get an infinite loop!
- do-while Loop - At Least Once — do-while is like while, but it runs the code FIRST, then checks the condition. This guarantees the code runs at least once, even if the condition is false from the start.
- break - Exit Early — 'break' immediately exits the current loop, even if the condition is still true. Use break when you find what you are looking for and do not need to keep looping.
- continue - Skip This Round — 'continue' skips the rest of the current iteration and jumps to the next one. Use continue when you want to skip certain items but keep processing the rest.
- map, where, reduce - No Loops Needed — Functional methods like map(), where(), and reduce() can replace many loops with cleaner, more readable code. They describe WHAT you want, not HOW to loop. Dart developers prefer these over manual loops when possible.
- Nested Loops — A loop inside a loop. The inner loop runs completely for each iteration of the outer loop. Use for grids, tables, or comparing every pair of items. Be careful with performance -- nested loops multiply the work!
Code example
void main() {
// for loop - repeat a specific number of times
print('=== Countdown ===');
for (var i = 5; i >= 1; i--) {
print(i);
}
print('Go!');
// for-in - iterate through a collection
print('\n=== Shopping List ===');
var shopping = ['milk', 'bread', 'eggs', 'butter'];
for (var item in shopping) {
print('Buy: $item');
}
// forEach - functional style
print('\n=== Uppercase ===');
shopping.forEach((item) => print(item.toUpperCase()));
// while loop - repeat until condition is false
print('\n=== Guessing Game ===');
var secret = 7;
var guess = 1;
while (guess != secret) {
print('Tried $guess... wrong!');
guess++;
}
print('Got it! The number was $secret');
// break and continue
print('\n=== First Even ===');
var nums = [1, 3, 5, 4, 7, 2];
for (var n in nums) {
if (n.isOdd) continue; // skip odd numbers
print('First even: $n');
break; // stop after first find
}
// Functional alternatives to loops
print('\n=== Functional ===');
var scores = [85, 92, 78, 95, 88];
var passing = scores.where((s) => s >= 80).toList();
var average = scores.reduce((a, b) => a + b) / scores.length;
print('Passing: $passing');
print('Average: ${average.toStringAsFixed(1)}');
}Line-by-line walkthrough
- 1. The main function starts
- 2. A comment for the for loop section
- 3. Printing a header
- 4. A for loop counting down from 5 to 1
- 5. Printing each number in the countdown
- 6. Closing the for loop
- 7. Printing 'Go!' after the countdown ends
- 8.
- 9. A comment for the for-in section
- 10. Printing a header with a newline escape for spacing
- 11. Creating a list of shopping items
- 12. for-in loops through each item in the list
- 13. Printing each item with a 'Buy:' prefix
- 14. Closing the for-in loop
- 15.
- 16. A comment for forEach
- 17. Printing a header
- 18. forEach calls the arrow function on each item, printing it uppercased
- 19.
- 20. A comment for while loop
- 21. Printing a header
- 22. Setting the secret number to 7
- 23. Starting the guess at 1
- 24. The while loop continues as long as guess does not equal secret
- 25. Printing the current wrong guess
- 26. Incrementing guess by 1 for the next try
- 27. Closing the while loop
- 28. Printing when the correct number is found
- 29.
- 30. A comment for break and continue
- 31. Printing a header
- 32. Creating a list with mixed odd and even numbers
- 33. for-in loops through each number
- 34. If the number is odd, continue skips to the next iteration
- 35. If we reach here, the number is even -- print it
- 36. break stops the loop after finding the first even number
- 37. Closing the for loop
- 38.
- 39. A comment for functional alternatives
- 40. Printing a header
- 41. A list of test scores
- 42. where() keeps only scores 80 and above
- 43. reduce() sums all scores, then divides by count for average
- 44. Printing passing scores
- 45. Printing average formatted to 1 decimal place
- 46. Closing the main function
Spot the bug
void main() {
var total = 0;
for (var i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
total += i;
break;
}
print('Sum of odd numbers: $total');
}Need a hint?
Trace through the loop step by step. What happens on the very first iteration?
Show answer
The break statement exits the loop after adding just the FIRST odd number (1). The total will be 1, not the sum of all odd numbers. Fix: remove the break; line so the loop continues through all 10 numbers, skipping evens with continue and adding odds.
Explain like I'm 5
Imagine you have to write 'I will study hard' 100 times on the board. Would you write it one by one? No! A loop is like a magic pen that writes it for you 100 times automatically. A 'for' loop says 'do this exactly 100 times.' A 'while' loop says 'keep writing until the teacher says stop.' And 'break' is like the teacher saying 'OK, you can stop now, even if you have not finished all 100!'
Fun fact
An infinite loop (a loop that never stops) once caused a $370 million satellite to crash into the ocean. The Ariane 5 rocket in 1996 had a software loop that tried to fit a 64-bit number into a 16-bit space, causing an overflow that made the rocket self-destruct 37 seconds after launch!
Hands-on challenge
Write a program that finds all prime numbers between 1 and 50. A prime number is only divisible by 1 and itself. Use a for loop with a nested loop to check divisibility. Hint: use break to exit the inner loop early when you find a divisor.
More resources
- Dart Loops (Dart Official)
- Dart Iteration (Dart Official)
- Dart Control Flow (Dart Official)