加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Tips 114:continue停止循环

(2012-08-31 18:19:52)
标签:

循环

forvalues

foreach

while

财经

分类: 简单编程

【问题】

有时候,做一个循环,但遇到某种情况下便中止。

有时候,中止下面命令,返回进行下一次循环。

有时候,中止全部循环。

【方法】

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.

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有