简体   繁体   English

do-while 循环的范围?

[英]Scope of do-while loop?

In Java, the body of a do-while loop and the loop condition do not belong to the same scope.在 Java 中, do-while循环的主体和循环条件不属于同一范围。 So the following code won't compile:所以下面的代码不会编译:

do {
    boolean b = false;
} while (b); // b cannot be resolved to a variable

But this code does make sense to me.但是这段代码对我来说确实有意义。

Also, I cannot find any pitfalls if the body and the condition are in the same scope;另外,如果身体和条件在同一范围内,我找不到任何陷阱; since the body will always get executed, and Java does not have Goto , I don't know how a variable declaration in the outermost do-while body scope could be skipped.由于主体总是会被执行,而 Java 没有Goto ,我不知道如何跳过最外面的do-while主体范围中的变量声明。 Even if it is possible, the compiler could always detect such possibility and then produce compile time errors.即使有可能,编译器也总能检测到这种可能性,然后产生编译时错误。

Is there any reason for this behavior (aside from keeping the do-while loop in the same format as while )?没有任何理由为这种行为(除了保持do-while在相同的格式为循环while )? I am really curious.我真的很好奇。 Thanks for any inputs!感谢您的任何投入!

Following your logic here is the case when b would not be defined prior to first usage:在这里遵循您的逻辑是在第一次使用之前不会定义b情况:

do {
    continue;
    boolean b = false;
} while (b); // b cannot be resolved to a variable

Note that very often boolean flags are a code small, try to avoid them rather than fight with them.请注意, boolean标志通常是一个小代码,尽量避免它们而不是与它们斗争。

Because that's one way scope is defined in Java;因为这是在 Java 中定义作用域的一种方式; inside {} is a new scope. {}是一个新的作用域。

IMO it wouldn't make much sense to special-case a single construct. IMO 对单个构造进行特殊处理没有多大意义。

In your example, the boolean variable b is scoped to the body of the do..while loop.在您的示例中,布尔变量b的范围限定为do..while循环的主体。 Since the conditional check is executed outside the body, the variable is out of scope.由于条件检查在主体之外执行,因此变量超出范围。 The correct construct would be:正确的构造是:

boolean b = false ; // Or whichever initial value you want
do {
    b = false;
} while (b);

You can write something like this if you want exit do-while block while boolean defined inside of do-while block.如果你想在 do-while 块内定义布尔值时退出 do-while 块,你可以写这样的东西。

do{
  boolean b;
  ...
  if(b){
    break;
  }
}while(otherCondition)  //or while(true)
do {
    boolean b = false;
}
while (b);

Because the boolean variable b is a local variable having scope only within a block .因为布尔变量b是一个局部变量,其作用域仅在块内

From the JLS :JLS

Every local variable declaration statement is immediately contained by a block.每个局部变量声明语句都立即包含在一个块中。

public class DoWhileLoopExample {

    public static void main(String[] args) {

        int i=1;
        do {
            System.out.println("Do While Loop Example");

        } while(i<1);
    }
}

It is basic scope rules.这是基本的范围规则。 Variables declared inside a set of curly braces {} go out of scope at the end of the braces.在一组大括号 {} 内声明的变量在大括号的末尾超出范围。 It defeats the standard scope rules and it would be extra work for the compiler to detect such a case.它违背了标准范围规则,编译器检测这种情况需要额外的工作。

The statement definition and your variable are not visible outside your statement block {} , which is why the compiler complains.语句定义和您的变量在您的语句块{}之外不可见,这就是编译器抱怨的原因。

The difference between the while and do ... while is that the do ... while is guaranteed to execute at least once and that is a more simpler way to write according to the JLS whiledo ... while之间的区别在于do ... while保证至少执行一次,这是根据JLS编写的更简单的方法

Regarding scope of a variable, it has to be visible, according to these rules or else you get a compile time error.关于变量的范围,根据这些规则,它必须是可见的,否则会出现编译时错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM