# LinuxCNC O-code Conditional

Instructions · `O if` · verification: source checked

The O-code conditional is a flow-control block in LinuxCNC NC programs. A group of statements shares one O-number, begins with if, may include elseif and else sections, and ends with endif. The interpreter executes the first branch whose condition is true; if no if or elseif condition is true, it executes the else statements.

## Specification

| Property | Value |
|---|---|
| Language | gcode |
| Code | if |
| Group | Conditional |
| Syntax | Onnn if [condition] ... Onnn elseif [condition] ... Onnn else ... Onnn endif |
| Dialect | LinuxCNC |

### Parameters

| Name | Description |
|---|---|
| O-number | Number used after O; the same number must be used for the if, any elseif or else, and endif statements in the group. |
| condition | Expression in square brackets after if or elseif; evaluated to true or false to choose the branch. |

## Study notes

- O codes use the letter O, not the number zero, as the first character; lowercase o is easier to distinguish from 0.
- The same O-number must be used for the if, any elseif or else, and the endif in one conditional group.
- Using the same number for more than one block, other words on a line with an O-word, or comments on a line with an O-word has undefined behavior.
- When an if condition is false, elseif conditions are evaluated in order until one is true; if none is true, the else statements execute.
- When a condition evaluates true, no further conditions in that group are evaluated.
- Labels on if, else, elseif, or endif that do not point to a matching block cause an error and abort the interpreter.
- Conditional examples use bracketed expressions such as [#31 EQ 3] and [#2 GT 5].

## Sources

1. [O Codes](https://linuxcnc.org/docs/html/gcode/o-code.html#ocode:conditional) (accessed 2026-10-26)

---

Source page: https://machineschema.com/instruction/ocode-conditional  
JSON: https://machineschema.com/instruction/ocode-conditional.json  
License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Cite Machine Schema and the original sources.
