Tips 114:continue停止循环
(2012-08-31 18:19:52)
标签:
循环forvaluesforeachwhile财经 |
分类: 简单编程 |
【问题】
有时候,做一个循环,但遇到某种情况下便中止。
有时候,中止下面命令,返回进行下一次循环。
有时候,中止全部循环。
【方法】
continue满足上面一个中止;
continue,break满足下面一个中止。
【例子】
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.