+ 8

What is the output?

int i=5; for(int i=0; i<7;i++){ } cout<<i;. //answer is 5 why not 7?

25th Oct 2017, 1:24 PM
Randeep Singh
Randeep Singh - avatar
4 Réponses
+ 11
5. Because the loop variable i is declared inside the loop and can only be accessed within the loop. So when you are trying to access i outside the loop, it will refer the first i which was declared outside.
25th Oct 2017, 1:30 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 6
thank you so much shamima and naitomea
25th Oct 2017, 1:33 PM
Randeep Singh
Randeep Singh - avatar
+ 4
It should be 5, because the 'i' in the for-loop has only a local range and won't affect the 'i' declared in main function.
25th Oct 2017, 1:28 PM
Shadow
Shadow - avatar
28th May 2018, 7:36 AM
Randeep Singh
Randeep Singh - avatar