Preference of "for(; ;)" over "while(1)"
Asked Answered
S

2

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?

Serbocroatian answered 30/6, 2013 at 20:44 Comment(18)
Where have you read this?Phonemic
I don't think there are important reasons. Personally, I think the former requires fewer magic constants, which I like.Biddie
@Phonemic In a C programming book.Serbocroatian
This is a duplicate of a question with lots of answers...Xavier
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.Edette
I prefer while (1) for it's simplicity, and for that reason it's also more readable as @H2CO3 says.Cataract
possible duplicate of Is "for(;;)" faster than "while (TRUE)"? If not, why do people use it?Decimalize
I'd advice you to have a good look at both and make your own choice; and you can always change your mind later.Cataract
@KlausByskovPedersen Don't think it's a duplicate, because this is not about speed, but a general query.Cataract
@Cataract mmmmm, the answers are definitely the same as the ones given here and in the comments.Decimalize
@KlausByskovPedersen Then you should say that the answers are duplicates, not the question ;-)Cataract
@Cataract haha, yeah, but I also think that the if not, why do people use it covers the general query of this question ;-)Decimalize
I have checked almost all the links and found that, this is a different issue.Serbocroatian
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)Audiogenic
@wildplasser; I think @ouah answer is very clear,though, it may be a matter of choice.Serbocroatian
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)Audiogenic
@Audiogenic 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.Grosberg
@H2CO3; I need some information. Hope you will reply when you have free time :)Serbocroatian
G
8

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).

Grosberg answered 30/6, 2013 at 20:46 Comment(6)
One other possible reason for preferring the for version: MSVC warns "conditional expression is constant" for while(1) but is happy with for(;;)Trice
@ouah; Man! From where are you collecting these information? +1Serbocroatian
Happy to know at least some reason is behind this.Serbocroatian
@Grosberg I guess its lots of experince!Serbocroatian
@Serbocroatian the use of the literal 1 in while (1)Grosberg
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.)Audiogenic
A
0

Another style to consider:

while (true) {
    /* ... */
}

You will have to include stdbool.h though, which was added in C99.

Note that while (1) and while (true) are equivalent, but the latter looks more modern and will generally be more readable for people who aren't as familiar with the C language.

Alcine answered 5/5 at 18:15 Comment(1)
This answer is not needed, since while(1) and while(true) are strictly equivalent in terms of how the argument of while is evaluated.Dourine

© 2022 - 2024 — McMap. All rights reserved.