Overview
For loops are used to execute a block of code repeatedly for a specific number of times. They are ideal when you know in advance how many iterations you need.
Basic Syntax
for (initialization; condition; increment) {
// Code to execute
}
Basic Incrementing Loop
The most common form of a for loop increments a counter:
for (int i = 1; i <= 5; i++) {
print('Iteración $i');
}
// Output: Iteración 1
// Iteración 2
// Iteración 3
// Iteración 4
// Iteración 5
The loop variable can be declared with int, var, or final. Use int when you want to be explicit about the type.
Decrementing Loop
You can decrement the counter to loop backwards:
for (var i = 5; i > 0; i--) {
print('Iteración: $i');
}
// Output: Iteración: 5
// Iteración: 4
// Iteración: 3
// Iteración: 2
// Iteración: 1
Custom Increment Values
You can modify the increment step to skip values:
for (var i = 0; i < 10; i += 2) {
print('Iteración: $i');
}
// Output: Iteración: 0
// Iteración: 2
// Iteración: 4
// Iteración: 6
// Iteración: 8
Use i += n to increment by n on each iteration, or i -= n to decrement by n.
Modifying Loop Variable Inside Body
You can alter the loop variable within the loop body:
for (var i = 0; i < 10; i++) {
print('Iteración: $i');
i++;
}
// Output: Iteración: 0
// Iteración: 2
// Iteración: 4
// Iteración: 6
// Iteración: 8
Modifying the loop variable inside the loop body can make your code harder to understand and maintain. Use custom increment values instead when possible.
Complete Example
void main() {
// Traditional increment form
for (int i = 1; i <= 5; i++) {
print('Iteración $i');
}
// Decrement in the loop
for (var i = 5; i > 0; i--) {
print('Iteración: $i');
}
// Altering the value of the literal i
for (var i = 0; i < 10; i++) {
print('Iteración: $i');
i++;
}
// Another form of altering the increment
for (var i = 0; i < 10; i += 2) {
print('Iteración: $i');
}
}
For Loop Components
Initialization
Executed once before the loop starts. Typically used to declare and initialize the loop counter.
Condition
Checked before each iteration. The loop continues as long as this evaluates to true.
Increment
Executed after each iteration. Usually increments or decrements the loop counter.
Body
The code block that executes on each iteration.
Common Patterns
Iterating from 0 to n-1
for (var i = 0; i < n; i++) {
// Code
}
Iterating from 1 to n
for (var i = 1; i <= n; i++) {
// Code
}
Iterating backwards
for (var i = n; i > 0; i--) {
// Code
}
Iterating with step size
for (var i = 0; i < n; i += step) {
// Code
}
For-in Loop
Dart also provides a for-in loop for iterating over collections like lists, sets, and maps:var numbers = [1, 2, 3, 4, 5];
for (var number in numbers) {
print(number);
}
Best Practices
- Use meaningful variable names instead of just
i when the loop purpose isn’t obvious
- Keep loop bodies simple and focused
- Consider using
for-in loops when iterating over collections
- Avoid modifying the loop variable inside the loop body
- Use
break to exit early and continue to skip iterations when needed