CNC Variables & Logic Control: # Parameters, IF, WHILE, DO, GOTO Explained with Real Code Examples
CNC variables and logic control bring true programming intelligence to your G-code.
Instead of running static programs, you can create dynamic, self-adaptive machining cycles using conditional logic — just like in modern programming languages.
📌 1. Introduction
Parametric programming in CNC is based on variables (#100, #500, etc.) and logical control commands like IF, WHILE, and GOTO.
This allows your program to make decisions, loop, and adapt in real time — without editing the code manually.
📌 2. Types of Variables
| Range | Type | Description |
|---|---|---|
| #1–#33 | Local | Temporary, used inside macros |
| #100–#199 | Common | User-defined, shared between programs |
| #500–#999 | Permanent | Saved even after power off |
| #1000+ | System | Machine parameters (axes, alarms, etc.) |
📌 3. Basic Example — Using Variables
#100 = 50. (X POSITION)
#101 = 25. (Y POSITION)
#102 = -10. (Z DEPTH)
G00 X#100 Y#101
G81 Z#102 R2. F200
G80
The machine drills a hole at (50,25) to depth -10 using variable values.
📌 4. Arithmetic Operations
| Symbol | Operation | Example |
|---|---|---|
+ | Addition | #100 = #101 + 10 |
- | Subtraction | #102 = #101 - 5 |
* | Multiplication | #103 = #101 * 2 |
/ | Division | #104 = #101 / 3 |
[] | Parentheses | #105 = [#100 + #101] * 2 |
📌 5. Conditional Logic — IF Statements
#100 = 25
IF [#100 GT 20] THEN #101 = 1
IF [#100 LT 20] THEN #101 = 0
| Operator | Meaning |
|---|---|
| EQ | Equal (=) |
| NE | Not equal (≠) |
| GT | Greater than (>) |
| LT | Less than (<) |
| GE | Greater or equal (≥) |
| LE | Less or equal (≤) |
📌 6. GOTO — Conditional Branching
#100 = 10
IF [#100 GT 5] GOTO 100
M30
N100 G81 Z-15. R2. F200
M99
If
#100 > 5, program jumps to lineN100and executes the drilling cycle.
📌 7. WHILE / DO / END Looping
Loops allow repeating a block until a condition is false.
#100 = 0
WHILE [#100 LT 5] DO1
G81 X[#100*20] Y0 Z-10. R2. F200
G80
#100 = [#100 + 1]
END1
Drills 5 holes spaced 20 mm apart — fully automated using logic.
📌 8. Nested Loops Example
#101 = 0
WHILE [#101 LT 3] DO1
#102 = 0
WHILE [#102 LT 3] DO2
G81 X[#101*30] Y[#102*30] Z-10. R2. F200
G80
#102 = [#102 + 1]
END2
#101 = [#101 + 1]
END1
Creates a 3×3 grid of drilled holes using nested logic — no manual coordinates required.
📌 9. DO Loops with Conditional Exit
#100 = 0
DO1
#100 = [#100 + 1]
IF [#100 GT 10] GOTO 500
G04 P1.
END1
N500 M30
Runs a 1-second pause 10 times, then jumps to program end.
📌 10. Using System Variables (Fanuc)
| Variable | Description |
|---|---|
| #3000 = n (message) | Alarm message |
| #3006 = n (pause) | Operator message |
| #4001–#4009 | Active motion modes |
| #5001–#5003 | Current machine position (X/Y/Z) |
| #149 | Current tool number |
Example:
IF [#5003 LT -50.] THEN #3000 = 1 (TOOL BELOW SAFE LIMIT)
Automatically stops if Z position is below -50 mm.
📌 11. Advanced Example — Auto Drilling Pattern with Variables
%
O1200 (AUTO DRILLING)
#101 = 6 (NUMBER OF HOLES)
#102 = 40. (CIRCLE RADIUS)
#103 = 0. (START ANGLE)
#104 = 360./#101
#105 = 0
WHILE [#105 LT #101] DO1
#106 = [COS[#103 + #105*#104]*#102]
#107 = [SIN[#103 + #105*#104]*#102]
G81 X#106 Y#107 Z-15. R2. F200
G80
#105 = [#105 + 1]
END1
M30
%
Automatically drills 6 equally spaced holes along a circular bolt pattern — pure parametric power.
📌 12. Haas Example — Simplified Conditional Logic
#100 = 15
IF [#100 LT 10] THEN GOTO 50
G04 P2.
N50 M30
Haas follows the same logic as Fanuc but supports inline condition evaluation.
📌 13. Siemens Example
IF EQ 1 THEN
G81 Z-10 F150
ELSE
G83 Z-30 Q5 F120
ENDIF
Siemens uses structured IF/ELSE/ENDIF blocks for cleaner macro logic.
📌 14. Heidenhain Example
LBL 1
Q1 = +10
IF +Q1 GT +5
CYCL DEF 200 DRILLING Q201=-10 Q206=200
ENDIF
LBL 0
Heidenhain macros use
Qvariables andIF/ENDIFsyntax, similar in logic but more readable.
📌 15. Real-World Application — Tool Life Counter
#500 = [#500 + 1]
IF [#500 GT 100] THEN #3006 = 1 (TOOL CHANGE REQUIRED)
Automatically alerts the operator after 100 tool uses — no PLC needed.
📌 16. Real-World Application — Smart Safe-Z Logic
IF [#5003 LT -80.] THEN #3000 = 5 (Z LIMIT EXCEEDED)
Monitors Z-axis position and raises an alarm if tool goes below safe level.
📌 17. Combining Conditions
#100 = 25
#101 = 50
IF [[#100 GT 20] AND [#101 LT 60]] THEN G65 P9001
Executes macro only if both conditions are true.
📌 18. Future Trends (2025–2030)
- AI macro optimization: Machine automatically adjusts parameters (# variables) based on cutting force sensors.
- Self-learning condition monitoring: WHILE loops adapt to real-time vibration or temperature input.
- Cloud-synced macro libraries: Share logic routines across connected CNC machines.
- Smart alarms: CNC predicts error states before they occur using conditional thresholds.
✅ Conclusion
Parametric programming transforms G-code into a real programming language.
By mastering variables, conditions, and loops, CNC machinists can build intelligent, modular, and fully automated machining strategies — capable of adapting to any geometry or condition without manual intervention.
Leave a comment