# Loop task

A Loop repeats a part of a job: once for each file, a number of times, or as long as a condition says. The part that is repeated is drawn inside the Loop's frame.

> The tasks inside the loop's frame run once per pass. Draw a line from the Body dot to the first of them; a task dropped into the frame joins the loop. A task with no line out ends the pass, and a line out of the frame ends the loop.

## The dots

| Dot | Leads to |
|---|---|
| **Body** | the first task inside the frame. Followed at the start of every pass. |
| the green dot, **Done** | what comes after the loop |
| the red dot, **Failure** | what happens when the loop fails |

A Loop without a Body line fails at once: "The Loop has no Body line: draw one from its Body dot to the first task inside the loop."

## Fields

| Field | What it does | On a new task |
|---|---|---|
| **Repeat** | **For each item**, **While a condition is true**, **Until a condition is true**, **A number of times** | For each item |
| **Items per pass** | for each item: how many items a pass gets. 1 is one file at a time. | 1 |
| **Times** | a number of times: how many passes | |
| **Also stop after a pass when the condition below is true** | for the two counted modes: an early way out | off |
| the first value, the comparison, the second value | the condition, for while and until, and for the early way out | |

> While is checked before each pass, until after each pass. The variables see the first item of the list, {items_count}, and {loop_number} of the pass.

So a **while** loop may run no pass at all, and an **until** loop always runs at least one.

### Limits

| Field | What it does | On a new task |
|---|---|---|
| **Most passes** | the loop stops after so many passes, whatever its mode | 1000 |
| **Most minutes** | the loop stops after this time | |
| **Pause between passes** | in **Milliseconds**, **Seconds** or **Minutes** | none |
| **At a limit** | **Fail (take the failure line)**, or **Go on (take the Done line)** | Fail |

Every loop has the limit of passes, so that a condition that never comes true cannot run for ever. For a loop over more than a thousand files one at a time, raise **Most passes**, or better, raise **Items per pass**.

### Results

| Field | What it does | On a new task |
|---|---|---|
| **When a pass fails** | **Stop the loop (take the failure line)**, or **Go on with the next pass** | Stop |
| **After the loop, go on with** | **The items as they came in**, or **What the passes left** | as they came in |

A pass fails when a task in it fails and has no failure line of its own. A failure that the body deals with itself, by a red line inside the frame, is not a failed pass.

## What a pass gets

| Mode | The list in a pass |
|---|---|
| For each item | the next item, or the next so many |
| A number of times, While, Until | the whole list, as the pass before left it |

## The loop's variables

Inside the frame:

| Variable | Is |
|---|---|
| `{loop_number}` | the pass, from 1 |
| `{loop_index}` | the pass, from 0 |
| `{loop_total}` | how many passes there will be, where that is known: for each item and for a number of times |
| `{loop_first}`, `{loop_last}` | `true` in the first and in the last pass |
| `{loop_elapsed}` | how long the loop has been running |

In a loop inside a loop, these are the inner loop's. The outer loop's are `{loop_<its task name>_number}` and so on.

## What it hands on

Down the Done line: the items as they came into the loop, or, if chosen, what the passes left behind, one pass after the other.

## Uses

**Each file by itself.** A Copy that fails stops at the file that failed. In a loop for each item, with **When a pass fails: Go on with the next pass**, every file gets its try, and the ones that failed are in the log.

**Batches.** For each item with **Items per pass** 500: a list of 100,000 is worked through in 200 passes, and the run can be resumed between them.

**Try again.** A number of times, with a pause and an early way out: see [Examples](Jobs-Examples.md#9-try-again-three-times).

**Wait for something.** Until `{items_count}` **Greater Than** `0`, with a Find in the body and a pause of a minute: look every minute until a file is there. **Most minutes** says when to give up.

## In the log

```
Loop each (7 item(s), 1 per pass): 7 pass(es).
Pass 3 had 1 error(s); going on with the next pass.
The loop finished after 7 pass(es); 7 item(s) go on.
```

Under Active Jobs, the tasks of the body show the pass they are in.

## Things to know

- **A line out of the frame ends the loop**, not only the pass. To end a pass early, let the path end inside the frame.
- **Branches inside a loop** are allowed, and loops inside loops.
- **Variables set in a pass stay** for the next pass and after the loop. A variable that shall be fresh in every pass has to be set at the start of the body.
- **After a restart**, a loop goes on with the pass it was at: "The loop resumes at pass 41 after a restart."
- **A number that is not one**: "The Loop setting … is not a whole number".
