Bird
Raised Fist0
CNC Programmingscripting~10 mins

Variables (#1, #2) in programs in CNC Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Variables (#1, #2) in programs
Start Program
↓
Assign #1 = 10
↓
Assign #2 = 20
↓
Use #1 and #2 in commands
↓
Program Ends
The program starts by assigning values to variables #1 and #2, then uses these variables in commands before ending.
Execution Sample
CNC Programming
O1000;
#1=10;
#2=20;
G01 X#1 Y#2 F100;
M30;
Assigns 10 to variable #1 and 20 to #2, then moves to X=10, Y=20 at feed rate 100.
Execution Table
StepActionVariable #1Variable #2Command Output
1Start programundefinedundefinedNo movement
2Assign #1=1010undefinedNo movement
3Assign #2=201020No movement
4Execute G01 X#1 Y#2 F1001020Move to X=10, Y=20 at feed 100
5Execute M30 (program end)1020Program stops
💡 Program ends after M30 command
Variable Tracker
VariableStartAfter Step 2After Step 3Final
#1undefined101010
#2undefinedundefined2020
Key Moments - 2 Insights
Why does #1 have a value before #2 is assigned?
Because #1 is assigned first at step 2, so it holds 10 while #2 is still undefined until step 3.
What happens if we use #1 or #2 before assigning a value?
The variable is undefined and may cause errors or unexpected behavior; see step 1 where variables are undefined.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of #2 at step 2?
A20
Bundefined
C10
D0
💡 Hint
Check the 'Variable #2' column at step 2 in the execution_table.
At which step does the program move to X=10, Y=20?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Command Output' column for the movement command in the execution_table.
If #1 was assigned 15 instead of 10, what would be the X coordinate at step 4?
A15
B10
C20
DUndefined
💡 Hint
Variable #1 controls X position; see variable_tracker for #1 values.
Concept Snapshot
Variables #1 and #2 store numeric values.
Assign with #1=10; #2=20;
Use in commands like G01 X#1 Y#2;
Variables must be assigned before use.
They help reuse values and make programs flexible.
Full Transcript
This CNC program example shows how variables #1 and #2 are assigned values and then used in movement commands. Initially, variables are undefined. Step 2 assigns 10 to #1, step 3 assigns 20 to #2. Step 4 uses these variables to move the tool to X=10, Y=20 at feed rate 100. The program ends at step 5 with M30. Variables must be assigned before use to avoid errors. Tracking variable values step-by-step helps understand program flow and output.

Practice

(1/5)
1. What is the main purpose of variables like #1 and #2 in CNC programs?
easy
A. To display messages on the screen
B. To create new tools for the machine
C. To control the speed of the machine
D. To store numbers that can be used and changed in the program

Solution

  1. Step 1: Understand variable role in CNC

    Variables like #1 and #2 hold values that can be reused and changed, making the program flexible.
  2. Step 2: Compare options to variable purpose

    Only To store numbers that can be used and changed in the program describes storing numbers for use and change, which matches variable use.
  3. Final Answer:

    To store numbers that can be used and changed in the program -> Option D
  4. Quick Check:

    Variables store numbers = A [OK]
Hint: Variables store values to reuse and change easily [OK]
Common Mistakes:
  • Thinking variables create tools
  • Confusing variables with machine speed control
  • Assuming variables display messages
2. Which of the following is the correct way to assign the value 50 to variable #1 in a CNC program?
easy
A. #1 = 50
B. set #1 50
C. #1 := 50
D. assign #1 50

Solution

  1. Step 1: Recall CNC variable assignment syntax

    In many CNC programming dialects, variables are assigned values using ':=' like #1 := 50.
  2. Step 2: Check each option

    Only #1 := 50 uses the correct syntax with ':='. Others use incorrect or unsupported commands.
  3. Final Answer:

    #1 := 50 -> Option C
  4. Quick Check:

    Assignment uses ':=' = A [OK]
Hint: Use ':=' to assign values to variables [OK]
Common Mistakes:
  • Using '=' instead of ':='
  • Using 'set' or 'assign' commands incorrectly
  • Omitting the '#' before variable number
3. Given the CNC program snippet:
#1 = 10
#2 = 5
G01 X#1 Y#2

What will be the command sent to the machine?
medium
A. G01 X#1 Y#2
B. G01 X10 Y5
C. G01 X15 Y0
D. G01 X5 Y10

Solution

  1. Step 1: Identify variable values

    #1 is assigned 10 and #2 is assigned 5.
  2. Step 2: Substitute variables in command

    Replace #1 with 10 and #2 with 5 in G01 X#1 Y#2, resulting in G01 X10 Y5.
  3. Final Answer:

    G01 X10 Y5 -> Option B
  4. Quick Check:

    Substitute #1=10, #2=5 = B [OK]
Hint: Replace variables with their values in commands [OK]
Common Mistakes:
  • Leaving variables as #1 and #2 in output
  • Adding values instead of substituting
  • Swapping variable values incorrectly
4. Identify the error in this CNC program snippet:
#1 = 20
#2 30
G01 X#1 Y#2
medium
A. Missing '=' sign in second variable assignment
B. Variables cannot be used in G01 commands
C. Variable numbers must be greater than 2
D. G01 command syntax is incorrect

Solution

  1. Step 1: Check variable assignments

    The first line assigns #1 = 20 correctly. The second line is missing '=' between #2 and 30.
  2. Step 2: Verify other statements

    Variables can be used in G01 commands, variable numbers can be #1 or #2, and G01 syntax is correct.
  3. Final Answer:

    Missing '=' sign in second variable assignment -> Option A
  4. Quick Check:

    Assignment needs '=' = A [OK]
Hint: Check all assignments have '=' sign [OK]
Common Mistakes:
  • Ignoring missing '=' sign
  • Thinking variables can't be used in commands
  • Assuming variable numbers must be higher
5. You want to move the CNC tool to X=15 and Y=25, but want to easily change these values later. Which program snippet correctly uses variables #1 and #2 for this?
hard
A. #1 = 15
#2 = 25
G01 X#1 Y#2
B. G01 X15 Y25
C. #1 = 25
#2 = 15
G01 X#2 Y#1
D. #1 = 15
#2 = 25
G01 X#2 Y#1

Solution

  1. Step 1: Assign variables to desired coordinates

    Assign #1 = 15 for X and #2 = 25 for Y to match the target position.
  2. Step 2: Use variables correctly in G01 command

    Use G01 X#1 Y#2 to move tool to X=15 and Y=25 using variables.
  3. Final Answer:

    #1 = 15
    #2 = 25
    G01 X#1 Y#2
    -> Option A
  4. Quick Check:

    Variables match coordinates = D [OK]
Hint: Assign variables to coordinates, then use in command [OK]
Common Mistakes:
  • Swapping variable values and positions
  • Using variables without assignment
  • Writing coordinates directly without variables
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard