Java 做 while, while

发布于 2024-08-25 07:12:12 字数 422 浏览 5 评论 0原文

当我运行此代码时,我可以期待什么行为:

do while(testA) {

    // do stuff

} while(testB);

它的行为会像:

do {
    while(testA) {
        // do stuff
    }    
} while(testB);

或者:

if(testA) {
    do {
        // do stuff
    } while(testA && testB);
}

或者完全意想不到的事情吗?

我问这个问题是因为我认为这是相当模糊的,而对于搜索这个主题的其他人来说,不是,因为我懒得测试它。

what behaviour can I expect when I run this code:

do while(testA) {

    // do stuff

} while(testB);

Will it behave like:

do {
    while(testA) {
        // do stuff
    }    
} while(testB);

Or:

if(testA) {
    do {
        // do stuff
    } while(testA && testB);
}

Or something totally unexpected?

I ask this question because I think this is quite ambiguous, and for other people searching on this topic, not because I am lazy to test it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

花之痕靓丽 2024-09-01 07:12:12

它相当于您的第一个块:

do {
    while(testA) {
        // do stuff
    }    
} while(testB);

Java语法在解析这个的时候是:

DoStatement:
    do Statement while ( Expression ) ;

Statement:
    WhileStatement

WhileStatement:
    while ( Expression ) Statement

Statement:
    Block

Block:
    { BlockStatements_opt }

可以看到Java编译器会把这个解析为 do; while(表达式);。这是解析您编写的代码的唯一有效方法。

请记住,它没有任何特殊规则来解析此构造。由于 do-while 循环的编写方式不寻常,它最终会让人们阅读起来感到困惑。在正常使用中,do-while 总是写成带有显式大括号的 do { ... } while

It is equivalent to your first block:

do {
    while(testA) {
        // do stuff
    }    
} while(testB);

The relevant parts of the Java grammar when parsing this are:

DoStatement:
    do Statement while ( Expression ) ;

Statement:
    WhileStatement

WhileStatement:
    while ( Expression ) Statement

Statement:
    Block

Block:
    { BlockStatements_opt }

You can see that the Java compiler will parse this as do <WhileStatement> while ( Expression ) ;. That's the only valid way to parse the code that you wrote.

Keep in mind that it doesn't have any special rule to parse this construct. It just ends up being confusing for a human to read due to the unusual way the do-while loop is written. In normal usage do-while is always written as do { ... } while with explicit curly braces.

吹泡泡o 2024-09-01 07:12:12

它确实有效,但行为取决于您正在测试的条件。例如这段代码:

  int i = 2;
  int j = 4;
  do while(j > 0) {
       i--; j--;
       System.out.println("i:" + i + " j:" + j);
 } while(i > 0);

输出:

i:1 j:3
i:0 j:2
i:-1 j:1
i:-2 j:0

所以它的工作原理如下:

while(j>0) {

}

而通过交换变量名称:

do while(i > 0) {
    //.. same as above
 } while(j > 0);

输出是:

i:1 j:3
i:0 j:2

看起来它的行为与第一种情况相同(即考虑第一个 while ),但是在这里,应用程序没有终止!


摘要:
testA不再不满意并且testB不满意时,代码像正常一样工作while(testA){} 循环。
但是:如果,当testA不再满足时,testB仍然满足< /strong>,循环不再执行,脚本不再终止。这仅适用于需要在循环内部更改“外部”循环的条件的情况。

更新:
在阅读其他答案后,我意识到这正是嵌套 do-while - while 循环的行为。
不管怎样,吸取的教训:不要使用这种语法,因为它会让你感到困惑;)

It works indeed, but the behaviour depends on what conditions your are testing. E.g. this code:

  int i = 2;
  int j = 4;
  do while(j > 0) {
       i--; j--;
       System.out.println("i:" + i + " j:" + j);
 } while(i > 0);

outputs:

i:1 j:3
i:0 j:2
i:-1 j:1
i:-2 j:0

So it works like:

while(j>0) {

}

Whereas by exchanging the variable names:

do while(i > 0) {
    //.. same as above
 } while(j > 0);

The output is:

i:1 j:3
i:0 j:2

It looks like it behaves the same as in the first case (i.e. the first while is considered), but here, the application is not terminating!


Summary:
At the time when testA is not satisfied anymore and testB is also not satisfied, the code works like a normal while(testA){} loop.
But: If, at the time when testA is no longer satisfied, testB is still satisfied, the loop is not executed any more and the script is not terminating. This only applies if the condition of the "outer" loop needs to be changed inside the loop.

Update:
And after reading other answer, I realize that this is exactly the behavior of the nested do-while - while loop.
Anyway, lesson learned: Don't use this kind of syntax because it can confuse you ;)

街角卖回忆 2024-09-01 07:12:12

它的行为就像

do {
    while(testA) { 
        // stuff
    }
} while(testB);

这样,该代码块是这样解析的:

在 testB 为 true 时执行 {a block of code}。其中 {a block of code} 是内部 while。

编写这样的代码肯定有点不常见:)

It behaves like

do {
    while(testA) { 
        // stuff
    }
} while(testB);

So, that block of code is parsed this way:

do {a block of code} while testB is true. Where {a block of code} is the inner while.

It's certainly a bit uncommon to write code like that :)

油饼 2024-09-01 07:12:12

答案是#1。只要testB满足,它就会继续循环,但如果testA不满足,它就不会执行代码。

The answer is #1. It will continue to loop as long as testB is satisfied but it will not execute the code if testA is not satisfied.

无尽的现实 2024-09-01 07:12:12

do {} while();”是一种构造,而“while(){}”是另一种构造。

不存在“do while”这样的东西;您无意中嵌套了它们,并利用了 {} 对于单个指令是可选的这一事实。

换句话说,这是合法的语法:

do System.out.println("Hello World") while (true);

if 语句一样,块被视为单个语句。

也就是说,您的第一个可能的答案是正确的,其中“while(){}”是外部“do {} while();”内的单个非括号内的内容。 ”

"do {} while();" is one construct, while "while(){}" is another.

There is no such thing as "do while"; you are inadvertently nesting them and taking advantage of the fact that {} is optional for single instructions.

In other words, this is legal syntax:

do System.out.println("Hello World") while (true);

And like in if-statements, a block is treated as a single statement.

Which is to say, your first possible answer is the right one, where "while(){}" is the single non-bracketed thing inside the outer "do {} while();"

此刻的回忆 2024-09-01 07:12:12

我不认为这段代码是合法的。

do while(testA) {

        // do stuff

    } while(testB);

I do not think this code is legal.

do while(testA) {

        // do stuff

    } while(testB);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文