1

I've noticed a strange statement while reading C code:

for (;;) {
    // some code
}

That is the first time I see this syntax but I assume this is equivalent to:

while(true) {
    // some code
}

The result is obviously the same (in terms of logic).

Then why is it written like that? Is there a difference at compilation time?

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
BlueMagma
  • 2,392
  • 1
  • 22
  • 46

2 Answers2

1

Both of them works like the same! If we follow the semantics of for loop Then for loop works like this:

for(_INITIALIZATION;_CONDITIONN_CHECKING;_OPEARTION){
    //Some code Here
}

Now each of the terms inside the for loop are independent of each other. So for(;;) means there is nothing condition to break the loop. Some other way of defining infinite for loops are

a)for(;;)
b)for(_INITIALIZATION;;)
c)for(;;operations)
d)for(_INITIALIZATION;;_OPERATIONS)
E)for(_INITIALIZATION;1;_OPERATIONS)
Kalu
  • 290
  • 2
  • 13
0

Yes it's the same, just somewhat semantically "more meaningful" for a reader than while(true)

AndreyS Scherbakov
  • 2,674
  • 2
  • 20
  • 27
  • In what way is it more meaningful ? I think the while(true) is more meaningful and easier to read – BlueMagma Nov 17 '15 at 11:29
  • As for personally me, I prefer while(true) finding it more elegant. But formally one should agree that 'it's unconditional' looks more concise than 'its condition is always true' :) – AndreyS Scherbakov Nov 17 '15 at 11:40
  • @BlueMagma: I know at least one popular compiler emits a warning _"conditional expression is constant"_ for `while (true)`. I try to write code that compiles cleanly (without warnings). – Blastfurnace Nov 17 '15 at 13:28