# O-code Looping

Instructions · `O-code Looping` · verification: source checked

O-code looping is a flow-control instruction in NC programs that repeats a group of blocks while a condition is true. It supports while/endwhile loops, which test the condition before each iteration, and do/while loops, which test after each iteration. O-break exits the loop immediately and O-continue skips to the next condition test.

## Specification

| Property | Value |
|---|---|
| Language | gcode |
| Code | O while |
| Group | flow control |
| Syntax | o<number> while [condition] ... o<number> endwhile; o<number> do ... o<number> while [condition] |
| Dialect | rs274ngc |

## Study notes

- O-codes use the letter O, not the digit zero, as the first character; lowercase o is recommended because it is easier to distinguish from 0.
- The while/endwhile form evaluates the condition before running the loop body; the do/while form runs the body first, then evaluates the condition.
- The O-number labels the loop: the same number appears on the opening while/do and the closing endwhile/while, e.g. o101 while ... o101 endwhile.
- Inside a while loop, O-break immediately exits the loop, and O-continue skips to the next evaluation of the while condition.
- Using the same O-number for more than one block, or placing other words or comments on the same line as an O-word, is undefined behavior.

## Sources

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

---

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