无限循环应用程序 - for(;;) [英] Infinite loop application - for(;;)

查看:111
本文介绍了无限循环应用程序 - for(;;)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  for(;;)
{
//写出代码
}

我明白它是什么以及它是如何和while(true)一样的循环结构,但是我的问题是...这是一个好的编程习惯,以及这种类型的循环结构应用于什么类型的应用程序?

有些情况下,这是所需的行为。例如,基于墨盒的游戏控制台上的游戏通常在其主循环中没有退出条件,因为程序没有操作系统退出;循环运行,直到控制台关闭。



另一个例子是当一个模块从另一个模块监听动作。这将需要一直听,所以听众必须听无限的时间,或直到程序关闭。像套接字,线程和UIComponents。

在无限循环的概念上没有不好的做法,但是如果不想要或损害系统的特性,它可以是考虑,就像当你创建一个无意的无限循环或者失去循环的程序控制一样。

使得无限循环成为一个好习惯:


  1. 确保这是一个理想的行为。如果它有停止条件,避免无限循环!

  2. 使用为(;;)而(真)
  3. 让它容错,拯救预期的异常并给予他们正确的治疗!

  4. 重要!做一个简单的无限循环!


I am trying to understand the concept behind the for loop example

for (;;)
{
//write code
}

I understand what it does and how it's the same looping structure as while(true), but my question is...is this good programming practice and for what sort of applications would this type of looping structure be applied to?

解决方案

There are a few situations when this is desired behavior. For example, the games on cartridge-based game consoles typically have no exit condition in their main loop, as there is no operating system for the program to exit to; the loop runs until the console is powered off.

Another example is when a module listen actions from another. It will need to listen all the time, so the listener have to listen for infinite time or until the program turn off. Like Sockets, Threads and UIComponents.

There isn't bad practice on the concept of infinite loop, but if it isn't wanted or prejudice your system's feature it can be considered, like when you create an unintentional infinite loop or lose program control for the loop.

To make the infinite loop a good practice:

  1. Make sure that it is a desired behavior. If it has stop condition, avoid infinite loop!
  2. Make it explicity with for(;;) or while(true). Avoid tautologies in expression, do it simple!
  3. Make it fault tolerant, rescuing expected exceptions and give to them the right treatment!
  4. And the most important! Make a simple infinite loop!

这篇关于无限循环应用程序 - for(;;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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