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

查看:221
本文介绍了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)是true,什么时候是false?

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

推荐答案

while(true)何时为true,什么时候为false?

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天全站免登陆