# writing infinite loop

This topic is 4496 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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)

##### Share on other sites
Maybe it's a trick question.

##### Share on other sites
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(;;);

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by simon10kIs 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.

##### Share on other sites
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

##### Share on other sites
So, basically no infinite loops wins over other.

##### Share on other sites
for(;;) is the winar

##### Share on other sites
Quote:
 Original post by graveyard fillafor(;;) is the winar

Plz explain?

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 10
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631751
• Total Posts
3002087
×