Lesson 9 of 51 beginner

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

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. 1. The main function starts
  2. 2. A comment for the for loop section
  3. 3. Printing a header
  4. 4. A for loop counting down from 5 to 1
  5. 5. Printing each number in the countdown
  6. 6. Closing the for loop
  7. 7. Printing 'Go!' after the countdown ends
  8. 8.
  9. 9. A comment for the for-in section
  10. 10. Printing a header with a newline escape for spacing
  11. 11. Creating a list of shopping items
  12. 12. for-in loops through each item in the list
  13. 13. Printing each item with a 'Buy:' prefix
  14. 14. Closing the for-in loop
  15. 15.
  16. 16. A comment for forEach
  17. 17. Printing a header
  18. 18. forEach calls the arrow function on each item, printing it uppercased
  19. 19.
  20. 20. A comment for while loop
  21. 21. Printing a header
  22. 22. Setting the secret number to 7
  23. 23. Starting the guess at 1
  24. 24. The while loop continues as long as guess does not equal secret
  25. 25. Printing the current wrong guess
  26. 26. Incrementing guess by 1 for the next try
  27. 27. Closing the while loop
  28. 28. Printing when the correct number is found
  29. 29.
  30. 30. A comment for break and continue
  31. 31. Printing a header
  32. 32. Creating a list with mixed odd and even numbers
  33. 33. for-in loops through each number
  34. 34. If the number is odd, continue skips to the next iteration
  35. 35. If we reach here, the number is even -- print it
  36. 36. break stops the loop after finding the first even number
  37. 37. Closing the for loop
  38. 38.
  39. 39. A comment for functional alternatives
  40. 40. Printing a header
  41. 41. A list of test scores
  42. 42. where() keeps only scores 80 and above
  43. 43. reduce() sums all scores, then divides by count for average
  44. 44. Printing passing scores
  45. 45. Printing average formatted to 1 decimal place
  46. 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

Open interactive version (quiz + challenge) ← Back to course: Flutter & Dart