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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:10:44  来源:igfitidea点击:

What condition does while(true) test? When is it true and false?

javaloopswhile-loop

提问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 breakto 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在特定条件成立时使用循环退出它们,但通常这种做法很草率,不推荐使用。如果不使用breakreturnSystem.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 == trueis 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 conditionis true. You can make that false by setting condition = false.

while(condition == true)将是 true 而conditionis 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 trueis ALWAYS trueThey are generally used when you have to do something until a certain condition is met. You then exit with the breakstatement

while(true)用于无限循环。它们将永远循环,因为trueis 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

  1. List item
  1. 项目清单

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) { 语句... }