而(1)vs.(;;) [英] while (1) vs. for ( ;; )
问题描述
以下逻辑上相同的
语句之间是否有任何偏好?
而(1){
vs.
for(;;){
我怀疑答案是不但是我想知道共识是什么?
这样当我输入它时它就不再在我的脑海里眨眼了。
Mike
Should there be any preference between the following logically equivalent
statements?
while (1) {
vs.
for ( ;; ) {
I suspect the answer is "no" but I''d like to know what the consensus is
so that it doesn''t blink through my mind anymore when I type it.
Mike
推荐答案
你是对的。答案是不。他们都会做同样的事情。
You''re right. The answer is no. They''ll both do the exact same thing.
在文章< pa *************** *************@ioplex.com> ;,
Michael B Allen< mb ***** @ ioplex.com>写道:
In article <pa****************************@ioplex.com>,
Michael B Allen <mb*****@ioplex.com> wrote:
以下逻辑上等同的语句之间是否有任何偏好?
while(1){
vs.
for(;;){
我怀疑答案是否但是我想知道什么是共识
这样当我输入它时它就不再在我的脑海里眨眼了。
Should there be any preference between the following logically equivalent
statements? while (1) { vs. for ( ;; ) { I suspect the answer is "no" but I''d like to know what the consensus is
so that it doesn''t blink through my mind anymore when I type it.
Just我的意见,但我倾向于使用它们稍微不同的
上下文。
我倾向于使用while(1)当我循环播放某些东西时
将在不确定的未来时间发生,例如EOF或
警报信号。
我倾向于使用for(;;)当我在有限的时间内循环播放某些东西时会发生这种情况,但这对于通过循环控制变量表达的
表示不方便,例如迭代
a一组数据结构,直到检测到某个属性。
给它一个不同的倾斜:for(;;)传达迭代,而
而(1)传达重复这不一定是迭代的。
但是当两者最终相同时,选择金额不会超过人类的暗示读者。
-
Daylight是OSRAM SYLVANIA INC的商标。
Just my opinion, but I would tend to use them in slightly different
contexts.
I would tend to use while(1) when I was looping for something that
will occur at an indeterminate future time, such as EOF or an
alarm signal.
I would tend to use for(;;) when I was looping for something that
will occur within a bounded time, but which is inconvenient to
express through loop control variables, such as iterating through
a set of data structures until a certain property is detected.
To give it a different slant: for(;;) conveys "iteration", whereas
while(1) conveys "repetition" that is not necessarily iterative.
But as the two work out the same in the end, the choice amounts
to no more than a hint to a human reader.
--
Daylight is a trademark of OSRAM SYLVANIA INC.
风格问题,没有别的。
就个人而言,我倾向于''换(;;)'',但这只是我的风格。
它是'比如询问哪个更好:
if(){
}
或:
如果()
{
}
我出于各种原因专门使用第二种形式,但这是事情
的风格。不要改变代码。
Question of style, nothing else.
Personally, I tend to prefer ''for (;;)'', but that''s just my style.
It''s like asking which is better:
if () {
}
or:
if ()
{
}
I use the second form exclusively for various reasons, but it''s a matter
of style. Doesn''t change the code one bit.
这篇关于而(1)vs.(;;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!