Bird
Raised Fist0
CNC Programmingscripting~20 mins

Conditional statements (IF-THEN) in CNC Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Conditional Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a simple IF-THEN block
What will be the output of this CNC program snippet if the variable #100 is set to 5?
CNC Programming
IF [#100 GT 3] THEN
  #101 = 1
ENDIF
M30
ARuntime error due to undefined variable
BProgram ends with #101 = 0
CSyntax error due to missing ELSE
DProgram ends with #101 = 1
Attempts:
2 left
💡 Hint
Check if the condition #100 GT 3 is true when #100 is 5.
💻 Command Output
intermediate
2:00remaining
Behavior of nested IF-THEN statements
Given #200 = 10 and #201 = 5, what is the value of #202 after running this code?
CNC Programming
IF [#200 GT 5] THEN
  IF [#201 LT 10] THEN
    #202 = 100
  ENDIF
ENDIF
M30
A#202 = 0
B#202 = 100
CSyntax error due to nested IF
D#202 is undefined
Attempts:
2 left
💡 Hint
Both conditions must be true for #202 to be set.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in IF-THEN usage
Which option contains a syntax error in the IF-THEN statement?
AIF [#10 EQ 5] #20 = 1 ENDIF
BIF #10 EQ 5 THEN #20 = 1 ENDIF
CIF [#10 EQ 5] THEN #20 = 1 ENDIF
DIF [#10 EQ 5] THEN #20 = 1 ELSE #20 = 0 ENDIF
Attempts:
2 left
💡 Hint
Check if the THEN keyword is present after the condition.
🚀 Application
advanced
2:00remaining
Using IF-THEN to control spindle speed
You want to set spindle speed to 1000 if the tool number (#1) is 5, otherwise set it to 500. Which code snippet achieves this?
AIF [#1 EQ 5] THEN S1000 ELSE S500 ENDIF
B
IF [#1 EQ 5] THEN S1000 ENDIF
S500
C
IF [#1 EQ 5] THEN S1000
ELSE S500
ENDIF
DIF #1 EQ 5 THEN S1000 ELSE S500 ENDIF
Attempts:
2 left
💡 Hint
Check the correct placement of ELSE and ENDIF keywords.
🧠 Conceptual
expert
2:00remaining
Understanding IF-THEN evaluation order
Consider this code snippet: IF [#10 GT 0] THEN IF [#20 LT 5] THEN #30 = 1 ELSE #30 = 2 ENDIF ELSE #30 = 3 ENDIF If #10 = 0 and #20 = 3, what is the value of #30 after execution?
A#30 = 3
B#30 = 1
C#30 = 2
D#30 is undefined
Attempts:
2 left
💡 Hint
The outer IF condition controls whether the inner IF runs.

Practice

(1/5)
1. What does an IF-THEN statement do in CNC programming?
easy
A. It repeats a set of commands multiple times.
B. It stops the machine immediately.
C. It defines the speed of the machine.
D. It makes the program choose actions based on conditions.

Solution

  1. Step 1: Understand the purpose of IF-THEN

    IF-THEN checks a condition and runs commands only if the condition is true.
  2. Step 2: Compare with other options

    Repeating commands is done by loops, speed is set by other commands, and stopping is a different command.
  3. Final Answer:

    It makes the program choose actions based on conditions. -> Option D
  4. Quick Check:

    IF-THEN controls flow based on conditions [OK]
Hint: IF-THEN means "if this, then do that" [OK]
Common Mistakes:
  • Confusing IF-THEN with loops
  • Thinking IF-THEN sets machine speed
  • Assuming IF-THEN stops the machine
2. Which of the following is the correct syntax for an IF statement in CNC programming?
easy
A. WHEN condition DO commands END
B. IF (condition) { commands }
C. IF [condition] THEN commands ENDIF
D. if condition: commands

Solution

  1. Step 1: Identify CNC IF syntax

    CNC uses IF [condition] THEN ... ENDIF structure.
  2. Step 2: Check other options

    Options B and C use programming languages like C or Python syntax, A is not standard CNC syntax.
  3. Final Answer:

    IF [condition] THEN commands ENDIF -> Option C
  4. Quick Check:

    CNC IF syntax uses THEN and ENDIF [OK]
Hint: Look for THEN and ENDIF keywords in CNC IF syntax [OK]
Common Mistakes:
  • Using parentheses or braces like other languages
  • Omitting THEN or ENDIF keywords
  • Confusing IF with WHEN or DO
3. What will be the output of this CNC code snippet?
IF #100 GT 50 THEN
  #101 = 1
ELSE
  #101 = 0
ENDIF
PRINT #101
Assuming variable #100 is 60.
medium
A. 0
B. 1
C. Syntax Error
D. No output

Solution

  1. Step 1: Evaluate the condition #100 GT 50

    #100 is 60, which is greater than 50, so condition is true.
  2. Step 2: Determine the assigned value to #101

    Since condition is true, #101 is set to 1, then printed.
  3. Final Answer:

    1 -> Option B
  4. Quick Check:

    60 > 50 is true, so #101 = 1 [OK]
Hint: Check condition truth to find which branch runs [OK]
Common Mistakes:
  • Confusing GT (greater than) with LT (less than)
  • Ignoring ELSE branch
  • Assuming syntax error due to ELSE
4. Identify the error in this CNC IF statement:
IF #200 EQ 10
  G01 X100 Y100
ENDIF
medium
A. Missing THEN keyword after condition
B. Incorrect variable name #200
C. Missing ENDIF keyword
D. G01 command is invalid inside IF

Solution

  1. Step 1: Check IF statement syntax

    CNC IF requires THEN after the condition, missing here.
  2. Step 2: Verify other parts

    Variable name is valid, ENDIF is present, G01 is valid command inside IF.
  3. Final Answer:

    Missing THEN keyword after condition -> Option A
  4. Quick Check:

    IF must have THEN before commands [OK]
Hint: IF condition must be followed by THEN [OK]
Common Mistakes:
  • Forgetting THEN after IF condition
  • Thinking ENDIF is missing
  • Assuming commands can't be inside IF
5. You want the CNC machine to move to X=50 only if the tool number (#1) is 3 or higher. Which IF statement correctly does this?
hard
A. IF #1 GE 3 THEN G01 X50 ENDIF
B. IF #1 GT 3 THEN G01 X50 ENDIF
C. IF #1 LE 3 THEN G01 X50 ENDIF
D. IF #1 EQ 3 THEN G01 X50 ENDIF

Solution

  1. Step 1: Understand the condition "3 or higher"

    This means #1 is greater than or equal to 3 (GE 3).
  2. Step 2: Check each option's condition

    IF #1 GE 3 THEN G01 X50 ENDIF uses GE (greater or equal), B uses GT (greater than) which excludes 3, C uses LE (less or equal), D uses EQ (equal only).
  3. Final Answer:

    IF #1 GE 3 THEN G01 X50 ENDIF -> Option A
  4. Quick Check:

    GE means "3 or higher" [OK]
Hint: Use GE for "greater or equal" conditions [OK]
Common Mistakes:
  • Using GT excludes the value 3
  • Using LE or EQ does not cover all values 3 or higher
  • Confusing GE with GT
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