Jump to content

88 station

  • entries
    131
  • 评论
    3
  • 查看
    659,330

无条件循环&有条件循环


moh8888

829 查看

无条件循环&有条件循环

通常,如果要写一个无限循环,我们可以这么写:

			  while ( 1 ) {

					 /* ... ... */

			  }


    这么写完全正确。不过,while (1) 是一个有条件循环,每次循环都会判断 1 的真假性,

因为 1 永远为真,所以这个循环会无限执行。正因为 while (1) 是有条件循环,

所以在效率上比无条件循环要略孙一筹。哪怎么写无条件循环呢?

很简单。请看:


		 for ( /* 可以有代码 */; /* 必须为空 */; /* 可以有代码 */ ) {

			 /* ... ... */

		 }

上面的代码就是一个无条件循环,每次循环不用进行任何判断,所以效率比 while (1) 高。

来源: C/C++ 标准编程

0 评论


Recommended Comments

没有要显示的评论。

访客
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 创建新的...