8

My question is simple,thus I will not go in deep

can we use for() loop without condition like this

for(;;space+=1)
{
printf("  ");
break;
} 

2 Answers2

10

Of course you can. An empty condition is taken to evaluate to 1.

for (;;){/*ToDo - your code here*/} is idiomatic C.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Sorry for not including in my question but is this applied to other loops to??(like do-while and while loop) –  Jun 20 '17 at 11:03
  • 2
    `while(1){}` or `do {}while(1);` – arminb Jun 20 '17 at 11:05
  • 6
    No, they require *expressions* (rather than optional expressions). Folk will write `1` explicitly to achieve the same effect. – Bathsheba Jun 20 '17 at 11:05
  • It seems 'while(1){}' is a more cleaner approach to this than using for loop with missing conditions. (since both do the same) Correct me if I'm wrong. :) – Poornamith Oct 27 '18 at 06:13
2

Yes it is perfectly correct to do so.

But since you have provided a break immediately after printf, it will only execute once. I'm not sure whether this is what you wanted. But if so, then this works fine.

Chandrahas Aroori
  • 955
  • 2
  • 14
  • 27