crema
crema

Reputation: 11

semicolon and comma in for loops

for( i=0, i<3, i=i+1 )

for( i=0; i<4; i++ )

I do not understand why they are same.

for( i=0, i<3, i=i+1 )
will start with i=0, then i=0+1=1, i=1+1=2, i=2+1=3, then 3 is not satisfied with i<3, then should close. So in the end, it repeats only 3 times isn't it? (i=0, 1, 2)

for( i=0; i<4; i++ )
will start with i=0, then i=1, i=2, i=3, when reach i=4, 4 is not satisfied with i<4, then should close. So in the end, it repeats 4 times (i=0, 1, 2, 3).

Am I wrong?

Upvotes: 1

Views: 246

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

This

for( i=0, i<3, i=i+1 )

is invalid construction and will not be compiled.

So these constructions are not the same,:)

And if you will even substitute commas for semicolons in the first construction

for( i=0; i<3; i=i+1 )

in any case they will not be the same because the first loop will have only 3 iterations while the second loop will have four iterations.

Upvotes: 7

Related Questions