# Scan Order and One-Scan Delays

Source: https://rungs.dev/docs/scan-cycle/scan-order-and-one-scan-delays

> Why an output sometimes changes one scan late — how the order of your rungs or statements causes a one-scan delay in Ladder Logic and Structured Text, and how to watch it with Step.

Every so often a program looks completely correct, yet a light comes on one moment "too early", or a step in a sequence seems to get skipped. Almost always the cause is the same: the **order** in which your rungs or statements run. Get the order wrong and an output can come out **one scan late**.

> This builds on [The Scan Cycle](https://rungs.dev/docs/scan-cycle/the-scan-cycle) — the PLC evaluates your whole program, top to bottom, once per scan.

## Order makes a read fresh or stale [#order-makes-a-read-fresh-or-stale]

As the PLC works down your program, it updates each bit it writes. So a bit is **fresh** to everything **below** where it is written, but anything **above** has already run this scan and saw the value from _before_. That is invisible — until one rung or statement reads a bit that a _later_ one writes.

## When order delays an output [#when-order-delays-an-output]

Say `Light` should simply follow `Button`, passing through an in-between memory bit called `Flag`. Here it is wired so the line that **reads** `Flag` runs _above_ the line that **writes** it.

In **Ladder Logic** — `Light`'s rung is above `Flag`'s rung:

```ladder
XIC(Flag)OTE(Light);XIC(Button)OTE(Flag)
```

The same thing in **Structured Text** — `Light :=` runs above `Flag :=`:

```iecst
Light := Flag;
Flag := Button;
```

Logically `Light` = `Flag` = `Button`, so you would expect the light to come on the instant the button is pressed. It doesn't — it comes on **one scan later**. Here is why, scan by scan, with the button pressed at scan 2:

| Scan | `Button`        | `Flag` | `Light` | What happened                                                                                   |
| ---- | --------------- | ------ | ------- | ----------------------------------------------------------------------------------------------- |
| 1    | 0               | 0      | 0       | Idle.                                                                                           |
| 2    | **1** (pressed) | **1**  | 0       | `Flag` is set to 1 — but the `Light` line already ran _earlier this scan_ and read `Flag` as 0. |
| 3    | 1               | 1      | **1**   | The `Light` line now reads `Flag` as 1, so `Light` turns on.                                    |

The key line is **scan 2**: `Light` is computed _before_ `Flag` is updated, so it reads `Flag` at last scan's value (0) and has to wait a scan to catch up. You will hear this loosely called a _scan-order race condition_, though here it is perfectly predictable — it comes straight from the top-to-bottom order.

## Order decides whether the delay exists [#order-decides-whether-the-delay-exists]

Now flip the order so `Flag` is written _above_ where it is read.

In **Ladder Logic**:

```ladder
XIC(Button)OTE(Flag);XIC(Flag)OTE(Light)
```

In **Structured Text**:

```iecst
Flag := Button;
Light := Flag;
```

Now `Flag` is set first, and `Light` reads the fresh value in the **same** scan. `Light` follows the button with no delay:

| Scan | `Button`        | `Flag` | `Light` | What happened                                                                                           |
| ---- | --------------- | ------ | ------- | ------------------------------------------------------------------------------------------------------- |
| 1    | 0               | 0      | 0       | Idle.                                                                                                   |
| 2    | **1** (pressed) | **1**  | **1**   | `Flag` is set to 1; the `Light` line reads the fresh `Flag` _later the same scan_ and turns `Light` on. |

Compare **scan 2** with the first example: there `Light` was still 0, here it is already on. Same logic — one line swapped.

The rule of thumb, whichever language you use:

- A value written **above** where it is read updates in the **same scan**.
- A value written **below** where it is read is **one scan behind** — read above, written below.

Neither is "wrong" — sometimes a one-scan delay is exactly what you want. The point is to order your rungs or statements so they produce the timing you intend. (This is the companion to "later wins" from [The Scan Cycle](https://rungs.dev/docs/scan-cycle/the-scan-cycle): there, two places write the _same_ output; here, one place _reads_ what another _writes_.)

## Try it yourself [#try-it-yourself]

Build it in the [One-Scan Delay exercise](https://rungs.dev/exercises/one-scan-delay): drive one output that follows a button immediately and a second that follows it one scan later — the exact pattern from this page, in Ladder or Structured Text.

To watch the delay happen, use **Step** instead of **Start** (you can't catch a single scan at full speed — the simulator runs ten scans a second):

1. Click **Step** in the top toolbar to run exactly one scan and pause.
2. Toggle an input (like `Button`) in the I/O Panel.
3. Click **Step** again and watch the bits update one scan at a time.

In the first example above, you will see `Flag` turn on the scan you press the button, but `Light` stay off until the **next** Step — the one-scan delay, right in front of you.

`Step` shows you the values at the **end** of each scan; it runs a whole scan per click, not one rung at a time. So it tells you _when_ a bit changes — count the scans — while the order tells you _why_.

## Summary [#summary]

- Every scan, your program runs top to bottom. A line reads each value at whatever it holds at the moment that line runs.
- A value written **below** where it is read is **one scan behind**; written **above**, it is fresh in the same scan.
- That is why an output can land one scan late — a **one-scan delay** caused purely by order, no timer involved.
- The same rule applies to Ladder Logic (rung order) and Structured Text (statement order).
- Use **Step** to watch the change happen one scan at a time.