en:software:mdurosettacncsoftwaregcode:macro_programming:looping_branching

  GOTOn 
...
...     ; Code that will be executed
GOTO10  ; Code execution will jump to the line that starts with the label N10
...     ; Code that will NOT be executed
...
N10     ; Code execution will restart from this line 
...
...     ; Code that will be executed
...

IF [ CONDITION IS TRUE ] GOTOn
IF [#7 LT 0] GOTO65 ;If the value of variable #7 is less than 0, branch to block N65
...
... ;If the above condition is true, bypass this section and go down till N65
...
N65 ... ;Target block of the IF conditional statement

Two different syntaxes are supported:

 IF [ condition is true ] THEN [ argument ] 
IF [... Condition 1 is true ...] THEN1         ; Start of IF block 1
    ...
    <... Body of block 1 - Part 1 ...>
        IF [... Condition 2 is true ...] THEN2 ; Start of IF block 2
        ...
        <... Body of block 2 ...>
        ...
        END2                                   ; End of IF block 2
    ...
    <... Body of block 1 - Part 2 ...>
    ...
ELSE1
    ...
    <... Body of block 1 - else ...>
    ...
END1                                           ; End of IF block 1
...
IF [... Condition 1 is true ...] THEN1         ; Start of IF block 1
    ...
    <... Body of block 1 - Part 1 ...>
        IF [... Condition 2 is true ...] THEN2 ; Start of IF block 2
        ...
        <... Body of block 2 ...>
        ...
        END2                                   ; End of IF block 2
    ...
    <... Body of block 1 - Part 2 ...>
    ...
ELIF [... Condition 3 is true ...] THEN1
    ...
    <... Body of block 3 - elif ...>
    ...
ELIF [... Condition 4 is true ...] THEN1
    ...
    <... Body of block 4 - elif ...>
    ...
ELSE1
    ...
    <... Body of block 5 - else ...>
    ...
END1                                           ; End of IF block 1
...

WHILE [condition] DOn

While loops can be nested as follows

WHILE [... Condition 1 is true ...] DO1         ; Start of WHILE loop 1
    ...
    <... Body of Loop 1 - Part 1 ...>
        WHILE [... Condition 2 is true ...] DO2 ; Start of WHILE loop 2
        ...
        <... Body of Loop 2 ...>
        ...
        END2                                    ; End of WHILE loop 2
    ...
    <... Body of Loop 1 - Part 2 ...>
    ...
END1                                            ; End of WHILE loop 1
...

To exit a while loop BREAKn can be used.

WHILE [... Condition 1 is true ...] DO1         ; Start of WHILE loop 1
    ...
    ... IF [... Condition 2 is true ...] BREAK1 ; Exit from loop 1
    <... Body of Loop 1 ...>
    ...
END1                                            ; End of WHILE loop 1
...

As shown in the previous 2 sections multiline IF and WHILE statements need an ID.
This ID should follow the rules:

  1. it should be unique only inside the same subroutine or inside a file if the file has no subroutines
  2. it can be used multiple times in the same file if the statements are in different subroutines
  3. it can be used multiple times in different files

( © 2018 by RosettaCNC Motion                                           )
( file name: if_goto_and_while.ngc                                      )
( G-code example to show IF and GOTO use                                )
g17 g21 g40 g49 g54 g69 g90
G0 X0 Y0
 
#2 = 20
 
IF [#2 GE 3] GOTO11
G0 X100 ; Never executed because of the GOTO in the previous line
N11
 
; If the condition is True
; G1 is executed and the parameter 3 is set to 20 .
IF [#2 GE 3] THEN G1 X#2 #3 = 20
 
IF [#3 LE 3] GOTO19
G1 Y50 ; Executed because the condition in the previoud line is FALSE
N19
 
T3 M6
G91 ; Set to incremental mode
#3 = 0
WHILE [#3 LT 4] DO1
    #3 = [#3+1]
    G1 X10
END1
 
M2
  • Last modified: 2023/03/22 09:07
  • by 127.0.0.1