0

What does for(;1;) mean in C?

Is it the same as for(;;) in the sense of an infinite loop?

I know that the position where 1 is typed is the condition part of the variable, but in this for loop, I'm not convinced that it is indeed an infinite loop.

Lucas
  • 523
  • 2
  • 10
  • 20
cokecokecoke
  • 139
  • 1
  • 1
  • 7
    its loops like that until that 1 becomes a 0 :) – Keith Nicholas Mar 11 '15 at 07:27
  • It means `while (1)`, i.e., "run forever". The only way to exit this loop is with a `break` or a `return` statement somewhere inside it. In C++, `throw`ing an exception is another option. – barak manos Mar 11 '15 at 07:27
  • then why did the programmer who coded this source use ' for(;1;)-loop'? for(;;)-loop is much brief than the for(;1;)-loop. – cokecokecoke Mar 11 '15 at 07:31
  • Perhaps he or she realized that `for(;;)` would be a little obscure to other people reading their code. Readability is important, you know. Try to stick to it when possible. – barak manos Mar 11 '15 at 07:34
  • He tossed a coin. "Heads means for(;;) and tails means for(;1;)" - he said. Infinite loop can be written in like 1000 different ways. This is simply one of them. – Spook Mar 11 '15 at 07:34
  • I'd opt for `while(true)`. That doesn't leave much to interpretation. – juanchopanza Mar 11 '15 at 07:35
  • 5
    @barakmanos Heh, for me, `for(;;)` is actually almost the canonical "forever" loop. `for (; 1 ; )` is *less* readable for me. Always depends on your team, I guess. – Angew is no longer proud of SO Mar 11 '15 at 07:37
  • @juanchopanza: Wouldn't work in C, unless you define `true`. – barak manos Mar 11 '15 at 07:38
  • BTW, as much as this question looks silly to most of us, it makes sense for newbies to ask it, so I see no reason to downvote it (except, perhaps, the fact that OP could have searched for some description of the `for` keyword, but I doubt that he or she would have found an answer to the specific question at hand). – barak manos Mar 11 '15 at 07:40
  • @barakmanos The question was tagged C++, and I'd expect it would be easy to figure out how to transform the code into valid C. – juanchopanza Mar 11 '15 at 07:45
  • @juanchopanza: Was tagged both C and C+, and now I see that the C++ tag has been removed. – barak manos Mar 11 '15 at 07:54

3 Answers3

7

According to the C Standard (6.8.5.3 The for statement)

2 Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

So in fact you yourself instead of the compiler replaced the omitted expression-2 by nonzero constant 1.

So these two statements

for(;1;) { /*...*/ }

and

for(;;) { /*...*/ }

are fully equivalent.

In my opinion it is much better to write

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

instead of the shown above for statements.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
3

Be convinced. For loop works until the condition-part of the expression becomes false. And in C 1 means "true", which - obviously - will never become false.

Spook
  • 25,318
  • 18
  • 90
  • 167
0

Basically, Your both expressions for(;1;) and for(;;) will give you infinite loop.

MSDN says

All of the expressions of the for statement are optional; for example, the following statement is used to write an infinite loop:

for (; ; )
{
    // ...
}

So no criteria in condition of loop will give you a infinite loop.

On you other question for(;1;): you are not convinced that it will give you infinite loop.

It has to a infinite loop, Because result of condition statement is not equal to 0.

Following are some examples:

 for(;1;)   //Infinite loop
 for(;0;)       //Loop will not execute at all
 for(;-1;)       //Infinite loop.
Santosh Dhanawade
  • 1,756
  • 14
  • 29