+ 4

I want someone to explain to me why output 17

int result=0; for (int I=0;I<5;I++) if(I==3) result +=10; else result +=I; why output =17

2nd Aug 2018, 6:52 AM
abdallah abdarahim
abdallah abdarahim - avatar
5 Answers
+ 8
it's correct I = 0 result = 0 + 0 I = 1 result = 0 + 1 I = 2 result = 1 + 2 I = 3 result = 3 + 10 I = 4 result = 13 + 4 = 17 why are you expecting something else?
2nd Aug 2018, 7:05 AM
Davide Ferrero
Davide Ferrero - avatar
+ 7
The loop runs for values of l = 0,1,2,3,4. When I is 3, add 10 to result. For all other values of l which is not 3, add the value of l. 0 + 1 + 2 + 10 + 4 17
2nd Aug 2018, 7:04 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
0+1+2+10+4=17
2nd Aug 2018, 7:04 AM
Oma Falk
Oma Falk - avatar
+ 2
Thank you guys
2nd Aug 2018, 7:09 AM
abdallah abdarahim
abdallah abdarahim - avatar
+ 1
Were you trying to exit the loop after 10 is added to the result đŸ€š?
2nd Aug 2018, 11:34 AM
kartik