Bird
0
0
CNC Programmingscripting~10 mins

G-code program structure in CNC Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - G-code program structure
Start Program
Initialize Settings
Execute Commands
Check for End
Next Command
The G-code program starts, sets up machine parameters, runs commands step-by-step, and ends when the program finishes.
Execution Sample
CNC Programming
N10 G21 (Set units to mm)
N20 G90 (Absolute positioning)
N30 G01 X50 Y25 F100 (Move linearly to X50 Y25 at feed 100)
N40 M30 (End program)
This simple G-code program sets units, positioning mode, moves the tool, then ends the program.
Execution Table
Line NumberCommandActionMachine StateOutput
N10G21Set units to millimetersUnits=mmUnits set to mm
N20G90Set absolute positioning modePositioning=AbsolutePositioning set to absolute
N30G01 X50 Y25 F100Move tool linearly to X=50, Y=25 at feed 100Position=(50,25), Feed=100Tool moved to X50 Y25
N40M30End programProgram EndedProgram stopped
💡 Program ends at line N40 with M30 command
Variable Tracker
VariableStartAfter N10After N20After N30After N40
UnitsDefault (inches)mmmmmmmm
PositioningDefault (Incremental)IncrementalAbsoluteAbsoluteAbsolute
Position(0,0)(0,0)(0,0)(50,25)(50,25)
FeedDefaultDefaultDefault100100
Program StateNot startedRunningRunningRunningEnded
Key Moments - 3 Insights
Why does the tool move to X50 Y25 at line N30?
Because line N30 uses G01 (linear move) with absolute positioning set by G90 at N20, so the tool moves directly to coordinates X=50, Y=25.
What happens if M30 is missing at the end?
The program would not officially end, so the machine might wait or repeat commands depending on controller settings. See exit at N40 in execution_table.
Why is G21 important at the start?
G21 sets units to millimeters; without it, the machine might interpret distances in inches, causing wrong moves. This is shown in variable_tracker Units change after N10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the machine position after line N30?
A(50, 25)
B(0, 0)
C(100, 50)
D(25, 50)
💡 Hint
Check the Position column in the execution_table row for N30
At which line does the program officially end?
AN10
BN20
CN40
DN30
💡 Hint
Look at the Action and Output columns in execution_table for program end
If G90 at N20 was changed to G91, how would the position after N30 change?
APosition would be (50, 25)
BPosition would be relative to current, so (50, 25) added to previous
CPosition would reset to (0,0)
DPosition would not change
💡 Hint
G91 means incremental positioning, so moves add to current position (see variable_tracker Position changes)
Concept Snapshot
G-code program structure:
- Start with program setup (units, positioning)
- Execute movement and machine commands line by line
- Use N numbers for line labels
- End program with M30
- Commands affect machine state stepwise
Full Transcript
This visual execution shows a simple G-code program structure. The program starts by setting units to millimeters with G21, then sets absolute positioning mode with G90. Next, it moves the tool linearly to coordinates X=50 and Y=25 at feed rate 100 using G01. Finally, the program ends with M30. The execution table traces each line's command, action, and machine state changes. The variable tracker shows how units, positioning mode, tool position, feed rate, and program state change after each command. Key moments clarify why absolute positioning affects moves, the importance of ending the program, and unit settings. The quiz tests understanding of position after moves, program end line, and effect of changing positioning mode. This step-by-step trace helps beginners see how G-code commands control CNC machine behavior in order.