Java while(true) 测试什么条件?什么时候是真的和假的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22512830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What condition does while(true) test? When is it true and false?
提问by Gregg1989
I do no understand why anybody would use
我不明白为什么有人会使用
while(true) {
//do something
}
instead of
代替
boolean condition = true;
while(condition == true) {
//do something
}
The latter is super easy to understand, whilst the former is not.
后者非常容易理解,而前者则不然。
So, what condition does while(true) check? When is while(true) true, and when is it false?
那么,while(true) 检查什么条件?while(true) 什么时候为真,什么时候为假?
采纳答案by Michael Berry
When is while(true) true, and when is it false?
while(true) 什么时候为真,什么时候为假?
It's always true, it's never false.
它永远是真的,永远不会是假的。
Some people use while(true)
loops and then use break
to exit them when a certain condition is true, but it's generally quite sloppy practice and not recommended. Without the use of break
, return
, System.exit()
, or some other such mechanism, it will keep looping forever.
有些人使用while(true)
循环,然后break
在特定条件成立时使用循环退出它们,但通常这种做法很草率,不推荐使用。如果不使用break
,return
,System.exit()
,或者其它某种这样的机制,它会永远保持循环。
回答by sunmoon
while(true) is always true. Loop statements are executed all the time. If you have to break the loop, we have to use break statement.
while(true) 始终为真。循环语句一直在执行。如果你必须打破循环,我们必须使用break语句。
回答by RKC
condition == true
is also going to return a boolean which is 'true'.So using that directly instead of all that.
condition == true
还将返回一个布尔值,它是“真”。所以直接使用它而不是所有这些。
回答by Rohit Jain
while(true)
loop will of course always iterate. You've to manually break out of it using break
, or System.exit()
, or may be return
.
while(true)
循环当然会一直迭代。您必须使用break
、 或System.exit()
、 或 可能是手动打破它return
。
while(condition == true)
will be true while condition
is true
. You can make that false by setting condition = false
.
while(condition == true)
将是 true 而condition
is true
。您可以通过设置condition = false
.
I would never ever use while(condition == true)
at least. Instead just use while (condition)
. That would suffice.
while(condition == true)
至少我永远不会使用。而只是使用while (condition)
. 那就足够了。
回答by Brian
while(true)
is used to for infinite loops. They will loop forever because true
is ALWAYS true
They are generally used when you have to do something until a certain condition is met. You then exit with the break
statement
while(true)
用于无限循环。它们将永远循环,因为true
is ALWAYStrue
它们通常在您必须做某事直到满足某个条件时使用。然后你用break
语句退出
while(true) {
//attempt to satisfy some condition
if (satisfied) {
break;
}
}
回答by Amit
Though we never know when we encounter a situation where we need it. We can also have infinite for loop.
尽管我们永远不知道何时遇到需要它的情况。我们也可以有无限循环。
for(;;) {//Code here}
回答by shashi kumar verma
- List item
- 项目清单
while(True): statement if(condition): break This is only work in python In java you can write like that for same purpose
while(True): statement if(condition): break 这仅适用于python 在java中,您可以出于相同目的编写这样的代码
while(1==1) { Ststements... }
while(1==1) { 语句... }