27.03.2013 Views

SPSS® 12.0 Command Syntax Reference

SPSS® 12.0 Command Syntax Reference

SPSS® 12.0 Command Syntax Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

LOOP—END LOOP 835<br />

time, resulting in cases with I=1, J=3, and K increasing from 1 to 4. Once the second loop<br />

has completed three iterations, control passes back to the first loop, and the entire cycle<br />

is repeated for the next increment of #I.<br />

• Once the first loop completes four iterations, control passes out of the looping structures<br />

to END FILE. END FILE defines the resulting cases as a data file, the input program terminates,<br />

and the LIST command is executed.<br />

• This example does not require a LEAVE command because the iteration variables are<br />

scratch variables. If the iteration variables were I, J, and K, LEAVE would be required because<br />

the variables would be reinitialized after each END CASE command.<br />

Example<br />

* Modifying the loop iteration variable.<br />

INPUT PROGRAM.<br />

PRINT SPACE 2.<br />

LOOP A = 1 TO 3. /*Simple iteration<br />

+ PRINT /’A WITHIN LOOP: ’ A(F1).<br />

+ COMPUTE A = 0.<br />

END LOOP<br />

PRINT /’A AFTER LOOP: ’ A(F1).<br />

NUMERIC #B.<br />

LOOP B = 1 TO 3. /*Iteration + UNTIL<br />

+ PRINT /’B WITHIN LOOP: ’ B(F1).<br />

+ COMPUTE B = 0.<br />

+ COMPUTE #B = #B+1.<br />

END LOOP IF #B = 3.<br />

PRINT /’B AFTER LOOP: ’ B(F1).<br />

NUMERIC #C.<br />

LOOP C = 1 TO 3 IF #C NE 3. /*Iteration + WHILE<br />

+ PRINT /’C WITHIN LOOP: ’ C(F1).<br />

+ COMPUTE C = 0.<br />

+ COMPUTE #C = #C+1.<br />

END LOOP.<br />

PRINT /’C AFTER LOOP: ’ C(F1).<br />

NUMERIC #D.<br />

LOOP D = 1 TO 3. /*Iteration + BREAK<br />

+ PRINT /’D WITHIN LOOP: ’ D(F1).<br />

+ COMPUTE D = 0.<br />

+ COMPUTE #D = #D+1.<br />

+ DO IF #D = 3.<br />

+ BREAK.<br />

+ END IF.<br />

END LOOP.<br />

PRINT /’D AFTER LOOP: ’ D(F1).<br />

LOOP E = 3 TO 1. /*Zero-trip iteration<br />

+ PRINT /’E WITHIN LOOP: ’ E(F1).<br />

+ COMPUTE E = 0.<br />

END LOOP.<br />

PRINT /’E AFTER LOOP: ’ E(F1).<br />

END FILE.<br />

END INPUT PROGRAM.<br />

EXECUTE.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!