writing infinite loop

Started by
42 comments, last by Kitt3n 18 years ago
This was asked from my friend in an interview. Which is a efficient way of writing an infinite loop?? I guess all are same, what do you think guys?
Advertisement
I'm not sure what an "efficient" way would be to writing an infinite loop, since it is an infinite loop, but I'd guess: while(1){ } or for(;;){ } So yea, the question needs more context to make sense. (The question they asked, not you)
Maybe it's a trick question.
Is while(true); more efficient than while(1); ?

I would guess so but im not sure, my theory is that 1 would have to evalute to true first, not sure about for(;;);
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Is while(true); more efficient than while(1); ?

I would guess so but im not sure, my theory is that 1 would have to evalute to true first, not sure about for(;;); or maybe the bool would have to change to an int type.
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Quote:Original post by simon10k
Is while(true); more efficient than while(1); ?

I would guess so but im not sure, my theory is that 1 would have to evalute to true first, not sure about for(;;);


No. Even if true was a macro (or even a constant), it would be replaced at compile-time.
void slow_infinite_loop(){    double f = 0;    while(f < 1) {        f = rand_float();        f = secant(f) * cosine(f);        f /= 2.0;        //do stuff here    }}


If the infinite loop is intentional, then chances are it won't get much faster than it already is.

CM
So, basically no infinite loops wins over other.
for(;;) is the winar
FTA, my 2D futuristic action MMORPG
Quote:Original post by graveyard filla
for(;;) is the winar


Plz explain?

This topic is closed to new replies.

Advertisement