+ 1

For loop

I tried putting a semicolon after a for loop with no body and it WORKS! here is the structure: for(int i=1;i<=5;i++,cout<<i<<endl); My question is WHY??

18th Mar 2018, 3:07 PM
Sara
Sara - avatar
3 Respostas
+ 7
Because a null statement is legal and there are times you want to make use of it. I would have reversed your expressions to output the numbers 1 to 5 instead of 2 to 6.
18th Mar 2018, 3:38 PM
John Wells
John Wells - avatar
+ 3
got it, Thanks.
18th Mar 2018, 3:39 PM
Sara
Sara - avatar
+ 1
Technically the increment part is part of the body. The i++ is executed at the end of the code you have inside the body.
18th Mar 2018, 3:35 PM
Alex
Alex - avatar