在for循环声明中初始化变量的范围实际上是否超过了块范围? [英] Is the scope of a variable initialized in a for loop declaration actually more than just block scope?

查看:156
本文介绍了在for循环声明中初始化变量的范围实际上是否超过了块范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  for() int i = 0; i <100; i ++ / *`i`在这里可见* /){
/ *`i`在这里可见* /
}
/ *`i `这里不​​可见* /

一切都很好。 我们说 i 具有block作用域



然而,为什么在中为循环声明的变量在 i ++



例如,为什么 j 不在这里,当它也有block在 i + = j

 之前的时间段内(int i = 0; i <100; i + = j / *只有`i`在这里可见* /){
int j = 1;
*`i`和`j`在这里是可见的* /
}
/ *`i`和`j`在这里是不可见的* /

我已经看到很多关于 i 范围的问题, 范围在范围内循环括号。这是否意味着技术上还有另外一个范围,没有人谈论哪个是for循环声明范围?如果是这样的话,我感兴趣的是如何在Java或C#等规范中定义这个范围,以及它通常被程序员称为什么范围。

编辑:是,我明白我可以为(int j,i = 0; i <100; i + = j)声明循环为,但是仍然证明 for 循环声明的范围比它们的大括号高。

解决方案

原因是 JLS 6.3 以这种方式指定作用域:

lockquote
在基本
for语句的ForInit部分中声明的局部变量的作用域(第14.14.1节)包括以下所有内容:

$ ul

  • 它自己的初始化器

  • 在for语句的ForInit部分右边
    for语句的Expression和ForUpdate部分




    包含的语句
  • code> for
    body。对于在中为主体定义的变量没有特殊的范围规则。正常规则适用。 (他们也在JLS 6.3中。)




    这个语言设计背后的原因包括(我猜是<1> / sup>):


    • 如果您必须查看循环体内的变量
    • 供程序员理解。例如:对于(i = 1; i <10; i = i + j)
      $ b

       ; 
      if(i> 3){
      j = 42;

      $ do

      code $ $ $ $ $ $
      $
      $ b


      1 - 真正的原因只有在20世纪70年代C的设计者才知道。我严重怀疑Java设计者甚至不屑于考虑做这样的事情。

      2 - 循环中的东西已经够糟糕了身体可以修改一个循环变量。 :-(


      Consider a for loop with a counter:

      for (int i = 0; i < 100; i++ /* `i` is visible here */) {
          /* `i` is visible here */
      }
      /* `i` not visible here */
      

      All is well. We say that i has "block" scope.

      However, why is it that variables declared within the for loop are not accessible at i++?

      For example, why is j not in scope here, when it also has "block" scope and was declared in a time period that is before i += j?

      for (int i = 0; i < 100; i += j /* only `i` is visible here */) {
          int j = 1;
          /* `i` and `j` are visible here */
      }
      /* `i` and `j` are not visible here */
      

      I have seen many questions concerning the scope of i, but not any concerning the scope of j within the for loop parentheses. Does this mean that there is technically another scope that nobody talks about which is "for-loop declaration scope"? If so, I'm interested in how this scope is defined in specs such as Java or C# and what scope it is generally referred to as by programmers.

      Edit: Yes, I understand that I can declare the loop as for (int j, i = 0; i < 100; i += j), but that still demonstrates that for loop declarations have a higher scope than their curly brackets.

      解决方案

      The facial reason for this that JLS 6.3 specifies the scoping that way:

      The scope of a local variable declared in the ForInit part of a basic for statement (§14.14.1) includes all of the following:

      • Its own initializer
      • Any further declarators to the right in the ForInit part of the for statement
      • The Expression and ForUpdate parts of the for statement
      • The contained Statement

      The "contained statement" the for body. There are no special scoping rules for variables defined in a for body. The normal rules apply. (They are also in JLS 6.3.)


      The reasons behind this language design include (I guess1) the following:

      • It would be bad for readability if you had to look inside the loop body for variables declared there2.
      • The logic to determine whether the variable declared in the loop was definitely initialized would be hard to specify, and difficult for programmers to understand. Example:

        for (i = 1; i < 10; i = i + j) {
            int j;
            if (i > 3) {
                j = 42;
            }
            // do stuff
        }
        


      1 - The real reasons would only be known to the designers of C ... back in the 1970's. I seriously doubt that the Java designers even bothered to consider doing this kind of thing.

      2 - It is bad enough that something in the loop body could modify a loop variable. :-(

      这篇关于在for循环声明中初始化变量的范围实际上是否超过了块范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆