从我所知,for循环的范围,在它之后没有一组括号,只是一个语句。对? 那么如何来验证码:for循环的范围
for(int x = 0; x < 3; x++)
if(x < 2)
System.out.println("hello");
System.out.println("world");
给出了输出:
hello
hello
world
是本声明if
也被认为是for循环的一部分?当然是,我的问题是为什么?请问实际上是什么,其范围是正确的语句之后块,因为当修改这样上面的代码:
for(int x = 0; x < 3; x++)
if(x < 2) {
System.out.println("hello");
System.out.println("world");
}
给出了输出:
hello
world
hello
world
编辑:下面大部分的答案是关于解释上述代码中的流量控制,我已经知道了。我的问题是关于for循环范围的规则。
这个规则其实是这样的:一个无支撑的for
循环的范围是紧随其后的下一个语句块吗?
第二个'println'超出'for'范围,并在循环完成后执行。 – oldrinb 2012-08-17 04:09:37
看到我的答案,希望能解决你所问的问题 – oldrinb 2012-08-17 04:27:38