Stata 11 help for continue

help continue -------------------------------------------------------------------------------

Title

[P] continue -- Break out of loops

Syntax

continue [, break]

Description

The continue command within a foreach, forvalues, or while loop breaks execution of the current loop iteration and skips the remaining commands within the loop. Execution resumes at the top of the loop unless the break option is specified, in which case, execution resumes with the command following the looping command. See [P] foreach, [P] forvalues, and [P] while for a discussion of the looping commands.

Option

break indicates that the loop is to be exited. The default is to skip the remaining steps of the current iteration and to resume loop execution again at the top of the loop.

Examples

forvalues x = 1/10 { if mod(`x',2) { display "`x' is odd" continue } display "`x' is even" }

would produce the same results as

forvalues x = 1/10 { if mod(`x',2) { display "`x' is odd" } else { display "`x' is even" } }

The break option allows you to prematurely exit the loop.

forvalues x = 6/1000 { if mod(`x',2)==0 & mod(`x',3)==0 & mod(`x',5)==0 {

di "The least common multiple of 2, 3, and 5 is `x'" continue, break } }

The loop is executed over the values of 6 to 30 instead of to 1000.

Also see

Manual: [P] continue

Help: [P] foreach, [P] forvalues, [P] while; [P] if, [P] exit


© Copyright 1996–2009 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index