Conditional and Looping Constructs
**********************************

This chapter documents PSPP commands used for conditional execution, looping, and flow of control.

BREAK
=====

     BREAK.

BREAK terminates execution of the innermost currently executing LOOP construct.

BREAK is allowed only inside LOOP...END LOOP. See LOOP, for more details.

DO IF
=====

     DO IF condition.
             ...
     [ELSE IF condition.
             ...
     ]...
     [ELSE.
             ...]
     END IF.

DO IF allows one of several sets of transformations to be executed, depending on user-specified conditions.

If the specified boolean expression evaluates as true, then the block of code following DO IF is executed.  If it evaluates as missing, then none of the code blocks is executed.  If it is false, then the boolean expression on the first ELSE IF, if present, is tested in turn, with the same rules applied.  If all expressions evaluate to false, then the ELSE code block is executed, if it is present.

When DO IF or ELSE IF is specified following TEMPORARY (see TEMPORARY), the LAG function may not be used (see LAG).

DO REPEAT
=========

     DO REPEAT dummy_name=expansion....
             ...
     END REPEAT [PRINT].

     expansion takes one of the following forms:
             var_list
             num_or_range...
             'string'...
             ALL

     num_or_range takes one of the following forms:
             number
             num1 TO num2

DO REPEAT repeats a block of code, textually substituting different variables, numbers, or strings into the block with each repetition.

Specify a dummy variable name followed by an equals sign (=) and the list of replacements.  Replacements can be a list of existing or new variables, numbers, strings, or ALL to specify all existing variables.  When numbers are specified, runs of increasing integers may be indicated as NUM1 TO NUM2, so that 1 TO 5 is short for 1 2 3 4 5.

Multiple dummy variables can be specified.  Each variable must have the same number of replacements.

The code within DO REPEAT is repeated as many times as there are replacements for each variable.  The first time, the first value for each dummy variable is substituted; the second time, the second value for each dummy variable is substituted; and so on.

Dummy variable substitutions work like macros.  They take place anywhere in a line that the dummy variable name occurs.  This includes command and subcommand names, so command and subcommand names that appear in the code block should not be used as dummy variable identifiers.  Dummy variable substitutions do not occur inside quoted strings, comments, unquoted strings (such as the text on the TITLE or DOCUMENT command), or inside BEGIN DATA...END DATA.

New variable names used as replacements are not automatically created as variables, but only if used in the code block in a context that would create them, e.g. on a NUMERIC or STRING command or on the left side of a COMPUTE assignment.

Any command may appear within DO REPEAT, including nested DO REPEAT commands.  If INCLUDE or INSERT appears within DO REPEAT, the substitutions do not apply to the included file.

If PRINT is specified on END REPEAT, the commands after substitutions are made are printed to the listing file, prefixed by a plus sign (+).

LOOP
====

     LOOP [index_var=start TO end [BY incr]] [IF condition].
             ...
     END LOOP [IF condition].

LOOP iterates a group of commands.  A number of termination options are offered.

Specify index_var to make that variable count from one value to another by a particular increment.  index_var must be a pre-existing numeric variable.  start, end, and incr are numeric expressions (see Expressions.)

During the first iteration, index_var is set to the value of start. During each successive iteration, index_var is increased by the value of incr.  If end > start, then the loop terminates when index_var > end; otherwise it terminates when index_var < end.  If incr is not specified then it defaults to +1 or -1 as appropriate.

If end > start and incr < 0, or if end < start and incr > 0, then the loop is never executed.  index_var is nevertheless set to the value of start.

Modifying index_var within the loop is allowed, but it has no effect on the value of index_var in the next iteration.

Specify a boolean expression for the condition on LOOP to cause the loop to be executed only if the condition is true.  If the condition is false or missing before the loop contents are executed the first time, the loop contents are not executed at all.

If index and condition clauses are both present on LOOP, the index variable is always set before the condition is evaluated.  Thus, a condition that makes use of the index variable will always see the index value to be used in the next execution of the body.

Specify a boolean expression for the condition on END LOOP to cause the loop to terminate if the condition is true after the enclosed code block is executed.  The condition is evaluated at the end of the loop, not at the beginning, so that the body of a loop with only a condition on END LOOP will always execute at least once.

If neither the index clause nor either condition clause is present, then the loop is executed MXLOOPS (see SET) times. The default MXLOOPS is 40.

BREAK also terminates LOOP execution (see BREAK).

Loop index variables are by default reset to system-missing from one case to another, not left, unless a scratch variable is used as index. When loops are nested, this is usually undesired behavior, which can be corrected with LEAVE (see LEAVE) or by using a scratch variable as the loop index.

When LOOP or END LOOP is specified following TEMPORARY (see TEMPORARY), the LAG function may not be used (see LAG).