使用“while (true) {...}"有什么意义? [英] What's the point of using "while (true) {...}"?

查看:28
本文介绍了使用“while (true) {...}"有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么有些人在他们的代码中使用 while(true){} 块?它是如何工作的?

Why do some people use while(true){} blocks in their code? How does it work?

推荐答案

这是一个无限循环.在每次迭代时,将评估条件.由于条件是true,它总是...真的...循环将永远运行.退出循环是通过检查循环内的某些内容来完成的,然后在必要时中断.

It's an infinite loop. At each iteration, the condition will be evaluated. Since the condition is true, which is always... true... the loop will run forever. Exiting the loop is done by checking something inside the loop, and then breaking if necessary.

通过将中断检查放在循环内,而不是将其用作条件,这可以更清楚地表明您期望它运行直到某些事件发生.

By placing the break check inside the loop, instead of using it as the condition, this can make it more clear that you're expecting this to run until some event occurs.

一个常见的使用场景是在游戏中;您想继续处理动作和渲染帧,直到游戏退出.

A common scenario where this is used is in games; you want to keep processing the action and rendering frames until the game is quit.

这篇关于使用“while (true) {...}"有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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