Check Structures and Loops on Siemens SINUMERIK
The control processes NC blocks as standard in the programmed sequence. There are two ways to vary that sequence: check structures (IF, ELSE, ENDIF, LOOP, FOR, WHILE, REPEAT) and program jumps and branches (GOTOS, GOTOB, GOTOF, GOTO, GOTOC, CASE). This page covers both, the difference between them, and when to use which.
In practice this means: instead of writing the same hole forty times you build a loop; you write one program that machines differently depending on part size; you prevent drilling during block search; you stop the program with an alarm when a probe measurement is out of tolerance. All of it is done with check structures.
The rules first: the limits of check structures
The manual issues a warning before it explains anything else. Programs written without knowing it fail on the machine with alarms that look unrelated:
Supplementary conditions
| Rule | What it means on the machine |
|---|---|
| The check structure cannot be used program-wide | A check structure cannot start in the main program and end in a subprogram; every level must close within itself |
| A nesting depth of up to 16 check structures can be set up on each subprogram level | Deeper nesting requires an additional subprogram level |
| Blocks with check structure elements cannot be suppressed (skip levels) | Writing /1 IF ... has no effect |
| Jump markers (labels) are not permitted in blocks with check structure elements | LABEL: WHILE ... is invalid |
| Check structures are processed interpretively; when a loop end is detected, a search is made for the loop beginning | For this reason the block structure of a program is not checked completely in interpreter mode — an unbalanced ENDIF/ENDWHILE is not caught immediately |
| It is not generally advisable to use a mixture of check structures and program branches | Do not interleave the two in the same program section |
| Correct nesting can be checked when cycles are preprocessed | If you write your own cycles, use that check |
What the display shows while a loop runs
If only selected blocks are executed within a program loop, the current block display shows the last main run block before the program loop. So that the processed blocks are also visible — for diagnostic purposes, for example — the decoding single block SBL2 must be activated.
IF, ELSE, ENDIF — conditional statement and branch
- Conditional statement: the program block between
IFandENDIFis executed only when the condition is satisfied. - Branch: with
IF– block_1 –ELSE– block_2 –ENDIF, one of two program blocks is always executed: block_1 if the condition is satisfied, block_2 if it is not.
| Keyword | Meaning |
|---|---|
IF | Introduces the conditional statement or branch |
ELSE | Introduces the alternative program block |
ENDIF | Marks the end of the conditional statement or branch |
| <condition> | Logical expression that is evaluated as TRUE or FALSE |
The manual’s tool change subprogram example
This example is worth studying because it solves a real production problem: in program test mode and in real execution, the “current tool” has to be read from different places.
SUPA, so it is unaffected by any zero offset or frame.
2. The same line carries D0: tool compensation is deselected before approaching the tool change point.
3. STOPRE comes before the read operation — reading a system variable requires a preprocessing stop.LOOP and ENDLOOP — the endless loop
Endless loops are used in endless programs. At the end of the loop there is always a branch back to the beginning.
| Keyword | Meaning |
|---|---|
LOOP | Initiates the endless loop |
ENDLOOP | Marks the end of the loop and results in a return jump to the beginning of the loop |
The manual’s example is the most sensible use of an endless loop — waiting for the operator:
The message appears, M0 stops the program, and when the operator corrects the situation and presses NC Start the loop returns to the beginning — and stops again if the situation has not been fixed.
FOR … TO … and ENDFOR — the count loop
The count loop is used if an operation must be repeated with a fixed number of runs.
| Element | Meaning |
|---|---|
FOR | Initiates the count loop |
ENDFOR | Marks the end of the loop and results in a return jump to the beginning of the loop, as long as the end value of the count has still not been reached |
| <variable> | Count variable, incremented from the initial to the end value and increased by the value “1” at each run. Type: INT or REAL. Note: the REAL type is used if R parameters are programmed for a count loop; if the count variable is of the REAL type, its value is rounded to an integer. |
| <initial value> | Initial value of the count. Condition: the start value must be lower than the end value. |
| <end value> | End value of the count |
Example 1 — INTEGER variable and R parameter as count variable
Example 2 — production of a fixed quantity of parts
WHILE and ENDWHILE — condition at the start of the loop
For a WHILE loop, the condition is at the beginning of the loop. The WHILE loop is executed as long as the condition is fulfilled. If the condition is not fulfilled at the outset, the loop does not run at all.
| Keyword | Meaning |
|---|---|
WHILE | Initiates the program loop |
ENDWHILE | Marks the end of the loop and results in a return jump to the beginning of the loop |
| <condition> | The condition must be fulfilled so that the WHILE loop is executed |
The manual’s example performs stepped plunging based on the actual axis position:
$AA_IW[...] is the axis setpoint in the workpiece coordinate system; each pass plunges 1 mm incrementally and the loop ends when the axis reaches −10.
REPEAT and UNTIL — condition at the end of the loop
For a REPEAT loop the condition is at the end of the loop. The REPEAT loop is executed once and repeated continuously until the condition is fulfilled.
| Keyword | Meaning |
|---|---|
REPEAT | Initiates the program loop |
UNTIL | Marks the end of the loop and results in a return jump to the beginning of the loop |
| <condition> | The condition that must be fulfilled so that the REPEAT loop is no longer executed |
Which loop should you use?
| Situation | Correct structure |
|---|---|
| The number of repetitions is known in advance (40 holes, 100 parts) | FOR ... TO ... ENDFOR |
| The number is unknown and the condition must be checked first (it may never run) | WHILE ... ENDWHILE |
| The body must run at least once, then the condition is checked | REPEAT ... UNTIL |
| Operator intervention is required; the program must not continue on its own | LOOP ... ENDLOOP |
| One of two alternatives | IF ... ELSE ... ENDIF |
| More than two branches, selected by an integer value | CASE ... OF ... DEFAULT |
Nested check structures — the manual’s example
This example combines three structures and solves a real safety problem: no drilling must take place during block search.
Reading the structure: the outer LOOP keeps working plate after plate; IF NOT $P_SEARCH ensures drilling happens only in normal execution; the inner WHILE machines the hole pattern until the X axis reaches 100; during block search only a message is displayed.
$P_SEARCH system variable is not checked, outputs and motions can be triggered during the search. Anyone writing their own cycles needs this pattern.Program jumps: GOTOS, GOTOB, GOTOF, GOTO, GOTOC
GOTOS — return jump to the start of the program
GOTOS is used to jump back to the beginning of a main program or subprogram in order to repeat the program. Machine data can be used to set that for every return jump:
- The program runtime is set to “0”.
- Workpiece counting is incremented by the value “1”.
| NC/PLC interface signal DB21, to DBX384.0 (control program branching) | Meaning |
|---|---|
| 0 | No return jump to the beginning of the program. Program execution is resumed with the next part program block after GOTOS. |
| 1 | Return jump to the beginning of the program. The part program is repeated. |
GOTOS internally initiates a STOPRE (pre-processing stop).
2. For a subprogram with data definitions (LUD variables), the jump goes to the first program block after the definition section, i.e. data definitions are not executed again. This is why the defined variables retain the value reached in the GOTOS block and are not reset to the standard values programmed in the definition section.
3. GOTOS is not available in synchronized actions and technology cycles.GOTOB, GOTOF, GOTO, GOTOC — jumping to a label
Jump markers (labels) are set in a program and can be jumped to from another location within the same program. Program execution is resumed with the statement that immediately follows the target marker.
| Command | Meaning |
|---|---|
GOTOB | Jump statement with jump target towards the beginning of the program |
GOTOF | Jump statement with jump target towards the end of the program |
GOTO | Jump statement with jump target search: the search is first made in the direction of the end of the program, then in the direction of the beginning |
GOTOC | Same effect as GOTO, except that alarm 14080 “Jump designation not found” is suppressed. Program execution is not interrupted if the search fails; it continues with the line following the GOTOC command. |
| Possible jump targets | Description |
|---|---|
| <jump marker> | A label set in the program with a user-defined name: <jump marker>: |
| <block number> | Main block or sub-block number (e.g. 200, N300) |
| STRING type variable | Variable jump target; the variable stands for a jump marker or a block number |
Rules for naming jump markers
- Jump markers are always located at the beginning of a block. If a program number exists, the jump marker is located immediately after the block number.
- Number of characters: minimum 2, maximum 32.
- Permissible characters: letters, numbers, underscores.
- The first two characters must be letters or underscores.
- The name of the jump marker is followed by a colon (“:”).
Supplementary conditions
- The jump target can only be a block with a jump marker or block number located within the program.
- A jump statement without a jump condition must be programmed in a separate block. This restriction does not apply to jump statements with jump conditions — in that case several jump statements can be formulated in one block.
- For programs with jump statements without jump conditions, the end of program
M2/M30does not necessarily have to be at the end of the program.
Example — jump with jump condition
N150 MA1: → N160 → N170 → N150 again → N190. The same block number (N150) is used twice and N180 is skipped. The version above is the correctly numbered example from the 01/2015 Job Planning manual. Duplicate block numbers break block search and jump behaviour.Example — indirect jump to a block number
Example — jump to a variable jump target
These two patterns let one program handle different tool or process scenarios: the target name is held in a variable and the decision is made inside the program.
CASE … OF … DEFAULT — multi-way branch
The CASE function provides the possibility of checking the actual value (type: INT) of a variable or an arithmetic function and, depending on the result, jumping to different positions in the program.
| Element | Meaning |
|---|---|
CASE | Jump statement |
| <expression> | Variable or arithmetic function |
OF | Keyword to formulate conditional program branches |
| <constant_1>, <constant_2> | Specified constant values for the variable or arithmetic function. Type: INT |
DEFAULT | Determines the jump target for the cases where the variable does not assume any of the specified constant values. If DEFAULT is not programmed, the block following the CASE statement is the jump target. |
GOTOF | Jump statement towards the end of the program. Instead of GOTOF, all other GOTO commands can be programmed. |
The manual’s example
- If VAR1+VAR2−VAR3 = 7, jump to the block with the jump marker “Label_1” (→ N40).
- If VAR1+VAR2−VAR3 = 9, jump to the block with the jump marker “Label_2” (→ N50).
- If the result is neither 7 nor 9, jump to the block with the jump marker “Label_3” (→ N60).
Program section repetition: REPEAT, REPEATB, ENDLABEL, P
Program section repetition allows you to repeat existing program sections within a program in any order. The program lines or sections to be repeated are identified by jump markers (labels).
REPEAT in this section is not the REPEAT ... UNTIL loop from the previous section. This REPEAT is a program section repetition and takes a repetition count through the P address. The same keyword serving two purposes is one of the most frequently confused points in Siemens programming.Four syntax forms
1. Repeat an individual program line
2. Repeat the section between the jump marker and the REPEAT statement
3. Repeat the section between two jump markers
4. Repeat the section between a jump marker and ENDLABEL
| Element | Meaning |
|---|---|
REPEATB | Command for repeating a program line |
REPEAT | Command for repeating a program section |
ENDLABEL | Keyword marking the end of a program section to be repeated. ENDLABEL can be used more than once in the program. If the line with ENDLABEL contains further operations, these are executed again on each repetition. |
P=<n> | Number of program section repetitions (type INT). After the last repetition, the program is resumed at the line following the REPEAT/REPEATB line. In the absence of a number being specified for P=<n>, the program section is repeated just once. |
Search direction of the jump marker
The program line identified by the jump marker can appear before or after the REPEAT/REPEATB statement. The search initially commences toward the start of the program; if the jump marker is not found in this direction, the search continues toward the end of the program.
Also: if the line with the jump marker contains further operations, these are executed again on each repetition.
Example 1 — repeat an individual program line
Example 2 — section between jump marker and REPEAT statement
Example 3 — section between two jump markers
Example 4 — machining the same drill positions with different technologies
This is the most powerful use of program section repetition: the drill positions are written once and then recalled for centering, drilling and tapping.
Further information and limits
- Program section repetitions can be nested. Each call uses a subprogram level.
- If
M17orRETis programmed during processing of a program section repetition, the repetition is canceled. The program is resumed at the block following the REPEAT line. - In the actual program display, the program section repetition is displayed as a separate subprogram level.
- If the level is canceled during the program section repetition, the program resumes at the point after the program section repetition call.
- It is not possible to nest the REPEAT statement with the two jump markers in parentheses. If the start jump marker appears before the REPEAT statement and the end jump marker is not reached before the REPEAT statement, the section between the start jump marker and the REPEAT statement will be repeated. The same restriction applies to the jump marker + ENDLABEL form.
- Check structures and program section repetitions can be used in combination, but there should be no overlap between the two. A program section repetition should appear within a check structure branch, or a check structure should appear within a program section repetition.
- If jumps and program section repetitions are mixed, the blocks are executed purely sequentially. For example, if a jump is performed from a program section repetition, processing continues until the programmed end of the program section is found.
- The REPEAT statement should appear after the traversing block.
Check structure or jump — which one?
The manual answers this directly:
| Check structures (IF, WHILE, FOR…) | Jumps (GOTOB, GOTOF, CASE) | |
|---|---|---|
| Readability | High — the structure is visible | Low — you have to trace labels |
| Speed in interpreter mode | Slower | Faster |
| Speed in precompiled cycles | No difference | No difference |
| Nesting limit | 16 per subprogram level | No structural limit, but complexity grows |
| Block suppression (skip) | Not possible | Possible |
| Labels | Not permitted in the same block | Required |
Practical advice: prefer check structures for readability and maintainability. If you write cycles and they will be precompiled, there is no difference anyway. Switch to jumps only where a very long, very frequently repeated loop makes interpreter-mode runtime a real problem. Do not mix the two in the same section — the manual explicitly advises against it.
The most common mistakes on the shop floor
- Treating the WHILE and UNTIL conditions as equivalent. In WHILE the condition must hold “to run”; in UNTIL it must hold “to stop running”. The same condition in both gives opposite results.
- Putting variable definitions inside a loop. Check structures work only in the statement section; header definitions cannot be executed conditionally or repeatedly.
- Writing a label in a check structure block. Jump markers are not permitted in blocks with check structure elements.
- Trying to suppress a check structure line with a skip level. Those blocks cannot be suppressed.
- Leaving an unbalanced ENDIF / ENDWHILE / ENDFOR. Because the block structure is not fully checked in interpreter mode, the error only appears at runtime.
- Exceeding the nesting limit of 16. Up to 16 check structures per subprogram level.
- Not knowing that GOTOS triggers a STOPRE. This can explain unexpected dwell in a program running in continuous-path mode.
- Expecting variables to be reset after GOTOS. LUD variables retain their values; the definition section is not executed again.
- Confusing the program section repetition REPEAT with the REPEAT loop. One takes a count with
P=<n>, the other ends withUNTIL. - Writing REPEAT without a P value. Without P the section is repeated just once — many users assume “not at all” or “forever”.
- Overlapping a check structure with a section repetition. One has to be entirely inside the other.
- Omitting the block-search check ($P_SEARCH). Motions and outputs can be triggered during block search.
Frequently asked questions
What is the equivalent of Fanuc macro B’s WHILE [condition] DO1 … END1?
WHILE <condition> ... ENDWHILE. SINUMERIK has no DO/END numbers; nested structures match by keyword.
Can I use a step of 2 in a FOR loop?
No. The count variable is increased by 1 at each run. If you need a different step, use WHILE and write the increment yourself.
Can I use an R parameter as the count variable?
Yes. The variable is then of the REAL type and, as the manual states, its value is rounded to an integer.
What happens if the initial value is higher than the end value?
The manual sets the condition clearly: the start value must be lower than the end value. FOR is not the right structure for the reverse case.
How do I stop a program from looping forever?
Put an exit condition (IF + GOTOF) inside LOOP–ENDLOOP, or convert the structure to WHILE. Otherwise, as the 2010 source notes, the program only stops on RESET or power-off.
When is GOTOC used?
In optional program sections where the jump target sometimes exists and sometimes does not. If the target is not found, alarm 14080 is suppressed and the program continues with the next line.
What happens if I omit DEFAULT in a CASE statement?
If the variable equals none of the specified constants, the block following the CASE statement becomes the jump target.
What is the difference between REPEAT and calling a subprogram?
REPEAT needs no separate file; it repeats a section within the same program. However, each REPEAT call uses a subprogram level and appears as a separate subprogram level in the actual program display.
Why does the block number on the screen not change inside a loop?
Because the current block display shows the last main run block before the loop. To see the processed blocks, activate the decoding single block SBL2.
Sources
- Siemens AG — SINUMERIK Advanced CNC Operation & Programming (Turkish edition), 05/2010: Section 4.8 Indirect programming (conditional jump, CASE branch), Section 4.9 Loops (endless loop LOOP–ENDLOOP, IF–ELSE–ENDIF, count loop FOR–ENDFOR, WHILE–ENDWHILE, REPEAT–UNTIL)
- Siemens AG — SINUMERIK 840D sl / 828D Job Planning, Programming Manual, 01/2015, document no. 6FC5398-2BP40-5BA2: Section 2.10 Program jumps and branches (2.10.1 GOTOS, 2.10.2 GOTOB/GOTOF/GOTO/GOTOC, 2.10.3 CASE…OF…DEFAULT), Section 2.11 Repeat program section (REPEAT, REPEATB, ENDLABEL, P), Section 2.12 Check structures (2.12.1 IF/ELSE/ENDIF, 2.12.2 LOOP/ENDLOOP, 2.12.3 FOR/ENDFOR, 2.12.4 WHILE/ENDWHILE, 2.12.5 REPEAT/UNTIL, 2.12.6 program example with nested check structures)