# Legacy - 03 - IF Loops

Flow control opcodes are opcodes that allow a script to either execute or ignore particular sections of code, or to terminate the script process depending on the value of the topmost stack items.

There are a variety of OPCODES available to trigger entry into IF loops including:

`OP_IF, OP_NOTIF, OP_ELSE`

### IF / NOTIF statements

IF statements are used to allow Bitcoin script to do comparative analysis on stack data items to control entry into IF loops. IF loops are the only means provided in Bitcoin script to perform different operations depending on previous processing.

The format of a simple IF loop is as follows:

`<Expression>`

`OP_IF`

`<True action>`

`OP_ENDIF`

<figure><img src="/files/Ud5dVJ34snct0zMuED0R" alt=""><figcaption></figcaption></figure>

In this example, if the operations performed in `<expression>` leave a non-zero item at the top of the stack, then the code in `<True action>` will be executed. Otherwise the script will jump to the opcode immediately after `OP_ENDIF`.

The alternative to `OP_IF` is `OP_NOTIF`.

`<Expression>`

`OP_NOTIF`

`<False action>`

`OP_ENDIF`

<figure><img src="/files/3ovtdwkOXvjJ1vh4dJhC" alt=""><figcaption></figcaption></figure>

When using `OP_NOTIF`, if the operations performed in `<expression>` leave a zero-value item at the top of the stack, then the code in `<False action>` will be executed. Otherwise the script will jump to the opcode immediately after `OP_ENDIF`.

Bitcoin grammar rules require that every `OP_IF / OP_NOTIF` must have a corresponding `OP_ENDIF`. Transactions that try to create outputs that do not adhere to this scripting rule are considered invalid and will not be accepted by the network.

### Using OP\_ELSE

Any IF loop can contain an ELSE statement which will cause the script to branch into one of two paths depending on the outcome of the expression being evaluated.

`<Expression>`

`OP_IF`

`<True action>`

`OP_ELSE`

`<False action>`

`OP_ENDIF`

### Multi-condition loops / Case statements

Using OP\_ELSE, IF loops can be nested allowing for complex nested functions to be developed. This allows for similar functionality to a case statement to be implemented.

`<Case 1 check>`

`OP_IF`

`<When 1 action>`

`OP_ELSE`

`<Case 2 check>`

`OP_IF`

`<When 2 action>`

`OP_ELSE`

`<Else action>`

`OP_ENDIF`

`OP_ENDIF`

<figure><img src="/files/meYatcZurxTIwokoCkNO" alt=""><figcaption></figcaption></figure>

An alternative method to nested IF loops is repeating IF loops in the code, although care must be taken to ensure that any 'ELSE' case is captured in an IF loop separately.

`<Case 1 check>`

`OP_IF`

`<When 1 action>`

`OP_ENDIF`

`<Case 2 check>`

`OP_IF`

`<When 2 action>`

`OP_ENDIF`

`<else check>`

`OP_IF`

`<Else action>`

`OP_ENDIF`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hub.bsvblockchain.org/higher-learning/bsv-academy/chapter-3-the-opcodes/03-if-loops.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
