Control Flow
For Loop
Accumulating a Total
A C-style for (init; cond; step) runs the body until the condition is false. += updates accumulator state.
Program
Play the program to sum the numbers 1 through 3.
for_loop.dart
void main() {
var total = 0;
for (var i = 1; i <= 3; i++) {
total += i;
}
print(total);
}
for
`for (init; cond; step)` is the classic counted loop.
++ and +=
`i++` post-increments; `total += i` adds in place.
accumulator
`total` carries state across iterations.