Home > database >  C : for; vs for{}
C : for; vs for{}

Time:04-18

I have seen several times for loop without instructions and they always use ; but is there a reason why no one use {} ?

Examples :

for(int i=0; i<5; i );

for(int i=0; i<5; i ){}

CodePudding user response:

There is no technical reason, it's mostly a matter of style. Both alternatives are logically identical.

It is true that with some fair frequency we get questions on Stackoverflow along the order of: why doesn't the following loop work correctly:

for (int i=0; i<10; i  );
{
   // Do something
}

So, one could argue that a trailing semicolon is easily to overlook, and can be error prone.

But there is no technical difference, otherwise.

  • Related