int main() { int i=0,j=1; for(i;i<5;i++); j=i+j; cout<<j; return 0; } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

int main() { int i=0,j=1; for(i;i<5;i++); j=i+j; cout<<j; return 0; }

Pls can some one explain this Why it prints 6??

2nd Apr 2020, 7:03 AM
BLACK GOD
BLACK GOD - avatar
2 Answers
+ 1
for-loop has no body in this code (only null statement - semicolon). for-loop iterates 5 times. After it has been finished "i" will be equal to 5, "j" will remain unchanged (equals 1) j=i+j is evaluated as j=5+1 = 6 6 is printed as result.
2nd Apr 2020, 7:28 AM
andriy kan
andriy kan - avatar
0
the for(i; i<5;i++); loop will stop when i=5, as it has no body(due to the semicolon after the statement), it will just increment i. and then j=i+j=5+1=6.
2nd Apr 2020, 7:23 AM
John Robotane
John Robotane - avatar