Python Loops: standard for, for-in, while & do-while
Dart offers versatile looping constructs for repetitive iteration. Standard `for (var i = 0; i < n; i++)` handles index-based counters; `for (final item in collection)` iterates cleanly over any `Iterable`; `while` executes while a condition holds true; and `do-while` guarantees at least one execution before testing the predicate.
"A `for-in` loop is like inspecting each item in a grocery bag one by one until the bag is completely empty."
Deep Dive: How It Works
`for-in` Iteration: The most idiomatic loop in Dart for collections (Lists, Sets, Maps keys/values).
`while` vs `do-while`: `while` checks upfront; `do-while` executes the body first before checking.
Closure Capture in for loops: Dart creates a new variable binding per iteration, making async closures inside `for` loops safe and predictable.
Syntax Blueprint
for (var i = 0; i < 5; i++) { ... }
for (final item in items) { ... }
while (hasMore) { ... }
do { ... } while (condition);Core iteration patterns across indexed ranges and iterable collections.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Modifying a List (adding/removing items) while iterating over it with `for-in`.Why it happens: Mutating an active iterator triggers a `ConcurrentModificationError`.
How to fix: Iterate over a copy (`for (final item in [...list])`) or use `list.removeWhere()`.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Calculate Factorial with While Loop
Compute the factorial of `int n = 5;` using a while loop.
Print `Factorial: $fact`.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.