Macro Flow Control

Normally, macro statements are processed in the order in which they appear. However, macro instructions can be used to alter the sequence in which lines are executed. Essentially these are two main techniques, branching and looping.

Branching is used to instruct GENER to process statements from a part of the macro other than the current line of code. Branching can be conditional, in which case GENER may or may not “jump” away from the current line, depending on some condition, or unconditional, in which case the jump always takes place.

Looping is a technique in which a series of statements which are specified once are executed several times in succession. GENER repeats the contents of the loop either a specified number of times, or until some condition is met.

The “IF” Statement

An “IF” statement defines one of more blocks of macro lines and determines which block is executed based on one or more conditional tests. The simplest format of an “IF” block is as follows:

IF / conditional_test
     statements…
ENDOF / IF

During the process, GENER will check the conditional test on the “IF” line. If it evaluates as TRUE, the subsequent block of statements will be executed, otherwise the control will jump to the “ENDOF/IF” line and the block of statements will be ignored. Multiple branching can be programmed by adding more conditional tests, as follows:

IF / conditional_test_1
     statements…
ELSEIF / conditional_test_2
     statements…
…
ELSE
     statements…
ENDOF / IF

Example:

IF/$T.EQ.1.OR.$T.EQ.10.OR.$T.EQ.20  $$ Tools 1, 10, 20
  INSERT/'G65 P1012'
ELSEIF/$T.GT.1.AND.$T.LT.10     $$ Tools 2, 3, 4, 5, 6, 7, 8, 9
  INSERT/'G65 P1016'
ELSEIF/$T.GE.11.AND.$T.LE.60        $$ Tools 11 through 60 except 20
  INSERT/'G65 P1015'
ELSE
  ERROR/8,'Invalid tool number' $$ Tools 61 and higher not available
ENDOF/IF

The “DO” Loop

The “DO” loop uses a variable (called a loop variable) which is iterated over a range of values specified using a start and end value. On each iteration, a block of statements is executed. The Format of the “DO” loops is as follows:

DO / variable = start, end  [ , step ]
    statements…
ENDOF / DO
  • The step argument is optional (if omitted, the default is 1)
  • The variable value is iterated between start and end values
  • All statements are executed on each iteration
  • The loop ends when variable reaches the end value
Examples:
DECLAR/GLOBAL,REAL,TOOLS(100)
DO/I=1,100
  TOOLS(I)=-1
ENDOF/DO

DECLAR/GLOBAL,REAL,TOOLS(100)
DO/I=100,1,-1
  TOOLS(I)=-1
ENDOF/DO

The “CASE”, “WHILE”, “REPEAT” and “EXIT” Statements

The “IF” & “DO” statements are probably the most common and intuitive statements that can be used in the macro language, but there are also other macro control statements that can be used. The “CASE” statement works similar to the “IF” statement, and the “WHILE” and “REPEAT” loops are similar to the “DO” loop. The “EXIT” statement is used to force exit out of a “DO”, “WHILE” or “REPEAT” loop.

To learn more about these loops and statements refer to the Post-Processor User Manual V23 ‘Flow Control in a Macro’.