2012-08-17 203 views
0

从我所知,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循环的范围是紧随其后的下一个语句块吗?

+1

第二个'println'超出'for'范围,并在循环完成后执行。 – oldrinb 2012-08-17 04:09:37

+0

看到我的答案,希望能解决你所问的问题 – oldrinb 2012-08-17 04:27:38

回答

3

您应该阅读Braceless if considered harmful。这篇文章是特意制作的,因为这样的例子;混乱无序的控制流程语句可能会让你挠头,特别是在误导缩进的情况下(比如你的例子)。

您粘贴的代码等同于以下,

for (int x = 0; x < 3; x++) { 
    if (x < 2) { 
    System.out.println("hello"); 
    } 
} 
/* outside of the for */ 
System.out.println("world"); 

正如你所看到的,循环迭代三次;前两个,它会打印"hello"。循环完成后,它将打印"world"


这个原因的理由在阅读Chapter 14 of the Java Language Specification时很清楚。实际上,根据§14.5,将视为语句是有意义的。

for (int x = 0; x < 3; x++) 
    if (x < 2) 
     System.out.println("hello"); 
     System.out.println("world"); 

综观if (§14.9)basicfor (§14.14.1)的描述中,我们看到的只是两者采取的声明;在这种情况下,我们可以看到我们的for声明包含if声明,声明本身封装了您的println("hello")声明。在for声明之后,您将获得您的println("world")声明。

for (int x = 0; x < 3; x++) 
    if (x < 2) { 
     System.out.println("hello"); 
     System.out.println("world"); 
    } 

在这里,我们看到了for声明体是if语句,它封装了包含声明,即双方你println语句。请注意,这确实与前者不一样。

希望这会为您解决问题。

3
First x=0 
Then if (x < 2) condition satisfies (again no braces, so only one statement executes) 
    Prints "hello" 
for loop continues 
x=1 
Then if (x < 2) condition satisfies (again no braces, so only one statement executes) 
    Prints "hello" 
    for loop continues 
x=2 
Then if (x < 2) condition NOT satisfies, so if statement won' execute, moves to print "world" 
    Prints "world" 

第一个片段会像对待:

for(int x = 0; x < 3; x++){ 
     if(x < 2) { 
      System.out.println("hello"); 
     } 
} 
      System.out.println("world"); 
1

循环之后的一行,条件是在循环体中考虑,条件你没有使用{}所以只能if被在考虑身体

for(int x = 0; x < 3; x++) 
     if(x < 2) 
      System.out.println("hello"); 
      System.out.println("world"); 

使输出hello hello world因为

唯一1行语句被考虑在for循环中,如果以后当循环结束world打印

它像

for(int x = 0; x < 3; x++) 
     { if(x < 2) 
      System.out.println("hello"); 
     } 
    System.out.println("world"); 

,并在

for(int x = 0; x < 3; x++) 
     if(x < 2) { 
      System.out.println("hello"); 
      System.out.println("world"); 
     } 

两个System.out.println("hello"); System.out.println("world");考虑在for循环

0

看到它这样...

forif有超过下一语句或封闭在{...}下一块控制。

如果{}缺失,只有下一条语句被认为是正文。

在第一种情况下,我已更正缩进,以便身体部位清晰可见。

for(int x = 0; x < 3; x++) 
    if(x < 2) 
     System.out.println("hello"); 
System.out.println("world"); 
0

当你不把{}仅下一行循环或条件语句被视为其范围的一部分了。

0

它正在做正确的事情。它为i = 0和1打印“hello”,然后循环结束并打印“world”。

我认为你受到取向混淆,以下是第一个应该如何看 -

for(int x = 0; x < 3; x++) { 
    if(x < 2) { 
     System.out.println("hello"); 
    } 
} 
System.out.println("world"); 

,第二个 -

for(int x = 0; x < 3; x++) { 
    if(x < 2) { 
     System.out.println("hello"); 
     System.out.println("world"); 
    } 
} 

更具可读性和易于理解的逻辑。