while(true) 测试什么条件?什么时候是真的和假的? [英] What condition does while(true) test? When is it true and false?

查看:22
本文介绍了while(true) 测试什么条件?什么时候是真的和假的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么有人会使用

I do no understand why anybody would use

while(true) {
   //do something
}

代替

boolean condition = true;
while(condition == true) {
   //do something
}

后者非常容易理解,而前者则不然.

The latter is super easy to understand, whilst the former is not.

那么,while(true) 检查什么条件?while(true) 什么时候为真,什么时候为假?

So, what condition does while(true) check? When is while(true) true, and when is it false?

推荐答案

while(true) 什么时候为真,什么时候为假?

When is while(true) true, and when is it false?

它永远是真的,永远不会是假的.

It's always true, it's never false.

有些人使用 while(true) 循环,然后在某个条件为真时使用 break 退出它们,但通常这种做法很草率,不推荐.如果不使用 breakreturnSystem.exit() 或其他类似机制,它将永远循环下去.

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) 测试什么条件?什么时候是真的和假的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆