How!! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How!!

[C++] int i=5; for(int i=0;i<7;i++) {} cout<<i; Output=5

16th Mar 2019, 6:07 AM
Amaresh Swain
Amaresh Swain - avatar
1 Answer
+ 9
If you were wondering why the output is 5 instead of 7, the answer was that you have two variables named <i>, one outside the for loop (initialized to 5), and the other one was declared within the for loop scope (initialized to zero). You were incrementing the <i> that was declared inside for loop, which will not affect the first <i> that was declared outside the for loop. Hth, cmiiw
16th Mar 2019, 7:03 AM
Ipang