为什么使用无限循环? [英] Why use infinite loops?

查看:168
本文介绍了为什么使用无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个海报询问了有关无限循环的首选语法

Another poster asked about preferred syntax for infinite loops.

后续问题:为什么在代码中使用无限循环?我通常看到这样的结构:

A follow-up question: Why do you use infinite loops in your code? I typically see a construct like this:

for (;;) {
  int scoped_variable = getSomeValue();
  if (scoped_variable == some_value) {
    break;
  }
}

这让你无法看到 子句中的scoped_variable值。 无限循环有什么其他用途?

Which lets you get around not being able to see the value of scoped_variable in the for or while clause. What are some other uses for "infinite" loops?

推荐答案

一个循环如:

while (true)
{
    // do something
    if (something else) break;
    // do more
}

让你跳出循环

如果你有一个复杂的条件,你可能还想要使用此样式使代码更清晰。

If you've got a complex condition, you might also want to use this style to make the code clearer.

这篇关于为什么使用无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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