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 IF is 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 LOOP is 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 IF clause 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 IF clause on LOOP allows 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.