# 6.5 The counted loop For

> `For` counts from one number to another and runs the body once for each value, with a control variable that moves by itself.

Web page: https://pseudolearn.app/en/spec/control/para

> **In one sentence**
>
> `For` counts from one number to another and runs the body once for each value, with a control variable that moves by itself.

## 6.5.1 How to write it

```pseudolearn
For «control» <- «inicial» To «final» ⟦Step «paso»⟧ Do
  «instrucciones»
EndFor
```

> **Parts**
>
> - `For` opens the loop and declares the «control» variable, of type `Integer`. It does not need to be defined first.
> - «initial», «final» and «step» are integer expressions.
> - `With Step` is optional. Without it, the step is 1.
> - `Do` separates the header from the body.
> - `EndFor` closes the loop.

## 6.5.2 How it works

Before the first round, the initial value, the final value and the step are computed **only once and in that order**. Then, on every round:

1. It checks whether the control has gone past the final value. The final value is **included**.
2. If not, the body runs.
3. The step is added to the control.

### Program: The 7 times table

```pseudolearn
Algorithm Tabla
  For i <- 1 To 5 Do
    Write "7 x ", i, " = ", 7 * i;
  EndFor
EndAlgorithm
```

**Output**

```text
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
```

*The program finishes normally.*

**Figure 6.5-1: The For counter**

A line with the values 1 to 6. The control starts at 1 and moves one by one; 5, the final value, is still included; on reaching 6 the loop ends.

The control walks the values from the initial one to the final one, both included. When it goes past the final value, the loop ends.

1. Step 1: 1, 5 and the step 1 are computed. The control starts at 1.
2. Step 2: Every round writes a line and adds the step: 2, 3, 4…
3. Step 3: With the control at 5 the body still runs: the final value is included.
4. Step 4: The control reaches 6, which is past the final value. The loop ends.

The direction depends **only on the sign of the step**. With a negative step it counts backwards. If the initial value is already past the final one, the body does not run:

### Program: Negative steps and empty loops

```pseudolearn
Algorithm Pasos
  For i <- 10 To 0 Step -5 Do
    Write i, " " Without Newline;
  EndFor
  Write "";
  For i <- 5 To 1 Do
    Write "nunca";
  EndFor
  Write "fin";
EndAlgorithm
```

**Output**

```text
10 5 0 
fin
```

*The program finishes normally.*

The control variable lives only inside the loop. When it ends, it stops existing.

> **Limit**
>
> - A zero step does not run the body and stops the program with `zeroStepInCountedLoop`.
> - The increment uses checked integer arithmetic, also after the last round: counting up to the largest integer overflows.

> **Common mistakes**
>
> The loop moves the control, not the body:
>
> ### Program: Changing the control inside the body
>
> ```pseudolearn
> Algorithm Tocar
>   For i <- 1 To 3 Do
>     i <- i + 1;
>   EndFor
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `controlVariableModified` (error, 3:5): Control variable i is modified inside the loop
>
> *The program finishes with an error.*
>
> ### Program: A zero step
>
> ```pseudolearn
> Algorithm Cero
>   For i <- 1 To 3 Step 0 Do
>     Write i;
>   EndFor
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `zeroStepInCountedLoop` (error, 2:3): The step of a counted loop cannot be zero
>
> *The program finishes with an error.*

> **Under the hood**
>
> The final value and the step are kept in temporaries before starting. That is why changing, inside the body, a variable used in the header does not alter the rounds.
>
> **Intermediate representation**
>
> ```text
> module Tabla en
> func suspendible @main() -> unit {
> b0() mass 1:
>   stmt.enter @2:3-4:9
>   %0 = const.i64 1 @2:12-2:13
>   %1 = const.i64 5 @2:17-2:18
>   %2 = const.i64 0
>   %3 = int.add %1, %2 @2:3-4:9
>   %4 = const.i64 1 @2:3-4:9
>   %5 = const.i64 0
>   %6 = int.add %4, %5 @2:3-4:9
>   loop.check_step %6 @2:3-4:9
>   jump b1(%0)
> b1(%7: i64) mass 0:
>   %8 = const.i64 0
>   %9 = int.cmp ge, %6, %8
>   branch %9, b2(), b3()
> b2() mass 0:
>   %10 = int.cmp le, %7, %3 @2:3-4:9
>   jump b4(%10)
> b3() mass 0:
>   %11 = int.cmp ge, %7, %3 @2:3-4:9
>   jump b4(%11)
> b4(%12: bool) mass 0:
>   decide.bool %12 @2:3-4:9
>   branch %12, b5(), b6()
> b5() mass 1:
>   stmt.enter @3:5-3:34
>   %13 = const.str "7 x " @3:11-3:17
>   %14 = const.str " = " @3:22-3:27
>   %15 = const.i64 7 @3:29-3:30
>   %16 = int.mul %15, %7 @3:31-3:32
>   out.write %13, %7, %14, %16, newline @3:5-3:34
>   stmt.exit @3:5-3:34
>   %17 = int.add %7, %6 @2:3-4:9
>   jump b7()
> b6() mass 0:
>   stmt.exit @2:3-4:9
>   return @5:1-5:13
> b7() mass 0:
>   task.yield_check @1:1-1:16
>   jump b1(%17) @1:1-1:16
> }
> ```

> **Note**
>
> Predict how many times `For i <- 0 To 10 With Step 3` runs. The answer, 4 (0, 3, 6, 9), shows that the final value is only included if it is reached.

**In short**
- `For` declares its control and moves it by itself.
- Initial, final and step are computed once.
- The final value is included; the sign of the step gives the direction.
- A zero step is a failure; touching the control, an error.

**See also**
- [6.6 Walking collections with For Each](https://pseudolearn.app/en/spec/control/para-cada.md)
- [6.3 The While loop](https://pseudolearn.app/en/spec/control/mientras.md)
- [3.4 Arrays](https://pseudolearn.app/en/spec/tipos/arreglos.md)
