IF Keyword (LOOP-END LOOP command)
The keyword IF and a logical expression can be specified on LOOP or on END LOOP to control iterations through the loop.
- The specification on
IFis a logical expression enclosed in parentheses.
Example
LOOP.
COMPUTE X=X+1.
END LOOP IF (X EQ 5). /*Loop until X is 5
- Iterations continue until the logical expression
on
END LOOPis true, which for every case is when X equals 5. Each case does not go through the same number of iterations. - This corresponds to the programming notion of
DO UNTIL. The loop is always executed at least once.
Example
LOOP IF (X LT 5). /*Loop while X is less than 5
COMPUTE X=X+1.
END LOOP.
- The
IFclause is evaluated each trip through the structure, so looping stops once X equals 5. - This corresponds to the programming notion of
DO WHILE. The loop may not be executed at all.
Example
LOOP IF (Y GT 10). /*Loop only for cases with Y GT 10
COMPUTE X=X+1.
END LOOP IF (X EQ 5). /*Loop until X IS 5
- The
IFclause onLOOPallows transformations to be performed on a subset of cases. X is increased by 5 only for cases with values greater than 10 for Y. X is not changed for all other cases.