Javascript权威指南5.6章节上面说因为continue语句在for语句跟while语句中的行为不一样,所以必须使用try、finally才能将while语句模拟的跟for语句一样
下面是我写的,但是在chrome devTool中运行没有结果?!不清楚为什么,求指点!
var simulate_for = function() {
var i = 0;
while ( i < 10 ) {
try {
if (i == 5) continue;
console.log(i);
}
finally {
i++;
}
}
};
simulate_for();
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
有结果啊,输出了 0 到 9,除了 5
在
for(int i = 0; i < 10; i++)
中,就算continue
,i
也会加1
。但在while
中,i++
一般在后面,continue
会跳过i++
,所以会有不一样的表现。题主加上finally
,while
的表现就和for
一样了。有结果啊。。。。
特地给你粘下来了~
i等于5的时候被continue了