> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/NormandoRamirezDelgado/6C-Febrero-2026/llms.txt
> Use this file to discover all available pages before exploring further.

# For Loops

> Learn how to use for loops to iterate and repeat code in Dart

## 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

```dart theme={null}
for (initialization; condition; increment) {
  // Code to execute
}
```

## Basic Incrementing Loop

The most common form of a for loop increments a counter:

```dart theme={null}
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
```

<Note>
  The loop variable can be declared with `int`, `var`, or `final`. Use `int` when you want to be explicit about the type.
</Note>

## Decrementing Loop

You can decrement the counter to loop backwards:

```dart theme={null}
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:

```dart theme={null}
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
```

<Tip>
  Use `i += n` to increment by `n` on each iteration, or `i -= n` to decrement by `n`.
</Tip>

## Modifying Loop Variable Inside Body

You can alter the loop variable within the loop body:

```dart theme={null}
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
```

<Warning>
  Modifying the loop variable inside the loop body can make your code harder to understand and maintain. Use custom increment values instead when possible.
</Warning>

## Complete Example

```dart theme={null}
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

<Steps>
  <Step title="Initialization">
    Executed once before the loop starts. Typically used to declare and initialize the loop counter.
  </Step>

  <Step title="Condition">
    Checked before each iteration. The loop continues as long as this evaluates to `true`.
  </Step>

  <Step title="Increment">
    Executed after each iteration. Usually increments or decrements the loop counter.
  </Step>

  <Step title="Body">
    The code block that executes on each iteration.
  </Step>
</Steps>

## Common Patterns

### Iterating from 0 to n-1

```dart theme={null}
for (var i = 0; i < n; i++) {
  // Code
}
```

### Iterating from 1 to n

```dart theme={null}
for (var i = 1; i <= n; i++) {
  // Code
}
```

### Iterating backwards

```dart theme={null}
for (var i = n; i > 0; i--) {
  // Code
}
```

### Iterating with step size

```dart theme={null}
for (var i = 0; i < n; i += step) {
  // Code
}
```

## For-in Loop

<Tip>
  Dart also provides a `for-in` loop for iterating over collections like lists, sets, and maps:

  ```dart theme={null}
  var numbers = [1, 2, 3, 4, 5];
  for (var number in numbers) {
    print(number);
  }
  ```
</Tip>

## 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
