semicolon and comma in for loops

£可爱£侵袭症+ 提交于 2021-02-05 08:59:28

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/26468703/semicolon-and-comma-in-for-loops

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!