LinuxCNC O-code Conditional

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.

Source checked7/7 values · 2026-10-26

Specification

1 source
Languagegcode
Codeif
GroupConditional
SyntaxOnnn if [condition] ... Onnn elseif [condition] ... Onnn else ... Onnn endif
DialectLinuxCNC

Parameters

NameDescription
O-numberNumber used after O; the same number must be used for the if, any elseif or else, and endif statements in the group.
conditionExpression 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].
↑↓ navigate↵ openesc close