5

I am asking this question only for the sake of knowledge(because i think it has nothing to do with beginner like me).
I read that C-programmers prefer

 for(; ;) {....}

over

 while(1) {....}

for infinite loop for reasons of efficiency. Is it true that one form of loop is more efficient than the other, or is it simply a matter of style?

haccks
  • 104,019
  • 25
  • 176
  • 264
  • Where have you read this? – PinnyM Jun 30 '13 at 20:45
  • 3
    I don't think there are important reasons. Personally, I think the former requires fewer magic constants, which I like. – Kerrek SB Jun 30 '13 at 20:45
  • @PinnyM In a C programming book. – haccks Jun 30 '13 at 20:46
  • 7
    This is a duplicate of a question with lots of answers... – Sergey Kalinichenko Jun 30 '13 at 20:46
  • 3
    They are equivalent, and in case you wonder, **no, none of them is faster than the other one,** and C programmers tend to prefer `for (;;)` because it's told to be "more idiomatic" for some reason, whereas I prefer `while (1)` because it's more readable. –  Jun 30 '13 at 20:47
  • 1
    I prefer `while (1)` for it's simplicity, and for that reason it's also more readable as @H2CO3 says. – meaning-matters Jun 30 '13 at 20:47
  • 1
    possible duplicate of [Is "for(;;)" faster than "while (TRUE)"? If not, why do people use it?](http://stackoverflow.com/questions/2611246/is-for-faster-than-while-true-if-not-why-do-people-use-it) – Klaus Byskov Pedersen Jun 30 '13 at 20:49
  • I'd advice you to have a good look at both and make your own choice; and you can always change your mind later. – meaning-matters Jun 30 '13 at 20:50
  • @KlausByskovPedersen Don't think it's a duplicate, because this is not about speed, but a general query. – meaning-matters Jun 30 '13 at 20:51
  • @meaning-matters mmmmm, the answers are definitely the same as the ones given here and in the comments. – Klaus Byskov Pedersen Jun 30 '13 at 20:53
  • 1
    @KlausByskovPedersen Then you should say that the answers are duplicates, not the question ;-) – meaning-matters Jun 30 '13 at 20:57
  • @meaning-matters haha, yeah, but I also think that the `if not, why do people use it` covers the general query of this question ;-) – Klaus Byskov Pedersen Jun 30 '13 at 21:03
  • I have checked almost all the links and found that, this is a different issue. – haccks Jun 30 '13 at 21:16
  • I prefer `while(1){...}` because it is shorter. The `for(;;) {...}` variant has some implied semantics, which is unnecessary. (BTW: for SQL, I prefer `... WHERE 1=1`, which is also very explicit) – wildplasser Jun 30 '13 at 23:28
  • @wildplasser; I think @ouah answer is very clear,though, it may be a matter of choice. – haccks Jun 30 '13 at 23:33
  • I beg to differ. The answer is bogus. And it is not an answer, just a pile of googled "facts". (which are irrelevant, since the whole issue is a just matter of taste anyway) – wildplasser Jun 30 '13 at 23:40
  • 1
    @wildplasser You are welcome to post your own answer if you doesn't like mine. If you feel it contains errors, feel free to comment and correct me. And FYI google was not use to post this answer. Having a knowledge of prior art and usage in programming usually adds a perspective on today's practice. – ouah Jul 01 '13 at 10:11
  • @H2CO3; I need some information. Hope you will reply when you have free time :) – haccks Oct 25 '13 at 15:33

1 Answers1

7

Both constructs are equivalent in behavior.

Regarding preferences:

The C Programming Language, Kernighan & Ritchie,

uses the form

for (;;)

for an infinite loop.

The Practice of Programming, Kernighan & Pike,

also prefers

for (;;)

"For a infinite loop, we prefer for(;;) but while(1) is also popular. Don't use anything other than these forms."

PC-Lint

also prefers

for (;;):

716 while(1) ... -- A construct of the form while(1) ... was found. Whereas this represents a constant in a context expecting a Boolean, it may reflect a programming policy whereby infinite loops are prefixed with this construct. Hence it is given a separate number and has been placed in the informational category. The more conventional form of infinite loop prefix is for(;;)

One of the rationale (maybe not the most important, I don't know) that historically for (;;) was preferred over while (1) is that some old compilers would generate a test for the while (1) construct.

Another reasons are: for(;;) is the shortest form (in number of characters) and also for(;;) does not contain any magic number (as pointed out by @KerrekSB in OP question comments).

Community
  • 1
  • 1
ouah
  • 142,963
  • 15
  • 272
  • 331
  • 1
    One other possible reason for preferring the `for` version: MSVC warns "conditional expression is constant" for `while(1)` but is happy with `for(;;)` – simonc Jun 30 '13 at 20:48
  • @ouah; Man! From where are you collecting these information? +1 – haccks Jun 30 '13 at 20:59
  • Happy to know at least some reason is behind this. – haccks Jun 30 '13 at 21:01
  • @ouah I guess its lots of experince! – haccks Jun 30 '13 at 21:03
  • @haccks the use of the literal `1` in `while (1)` – ouah Jun 30 '13 at 21:11
  • 1
    WRT `for(;;) is the shortest form (in number of characters)`. I think that human beings (and compilers) read tokens, not characters. `while(1)` is four tokens, `for(;;)` is five tokens (plus one implied_semantics token.) – wildplasser Jun 30 '13 at 23:54