Java 做 while, while
当我运行此代码时,我可以期待什么行为:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它相当于您的第一个块:
Java语法在解析这个的时候是:
可以看到Java编译器会把这个解析为
do; while(表达式);
。这是解析您编写的代码的唯一有效方法。请记住,它没有任何特殊规则来解析此构造。由于 do-while 循环的编写方式不寻常,它最终会让人们阅读起来感到困惑。在正常使用中,do-while 总是写成带有显式大括号的
do { ... } while
。It is equivalent to your first block:
The relevant parts of the Java grammar when parsing this are:
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.它确实有效,但行为取决于您正在测试的条件。例如这段代码:
输出:
所以它的工作原理如下:
而通过交换变量名称:
输出是:
看起来它的行为与第一种情况相同(即考虑第一个 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:
outputs:
So it works like:
Whereas by exchanging the variable names:
The output is:
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 andtestB
is also not satisfied, the code works like a normalwhile(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 ;)
它的行为就像
这样,该代码块是这样解析的:
在 testB 为 true 时执行 {a block of code}。其中 {a block of code} 是内部 while。
编写这样的代码肯定有点不常见:)
It behaves like
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 :)
答案是#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 iftestA
is not satisfied.“
do {} while();
”是一种构造,而“while(){}
”是另一种构造。不存在“
do while
”这样的东西;您无意中嵌套了它们,并利用了 {} 对于单个指令是可选的这一事实。换句话说,这是合法的语法:
与
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:
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();
"我不认为这段代码是合法的。
I do not think this code is legal.