+ 1

Help please

Can someone explain this code to me? How does the output come to be 17? int result = 0 for (int i = 0; i <5; i++) { if (i==3) { result += 10; } else { result += i; } } System.out.println(result);

25th Dec 2017, 6:30 PM
Marty
Marty - avatar
3 Réponses
+ 5
when I=1 result+=I so result is 1 when I=2 result+=I so result is 1+2 ==3 when I=3 result+=10 because I==3 so 3+10==13 when I =4 result+=4 si 13+4==17 and when I==5 the for loop stops running since I should be less than 5
25th Dec 2017, 6:38 PM
᠌᠌Code X
᠌᠌Code X - avatar
+ 4
yes the other answerer correctly
25th Dec 2017, 7:22 PM
John Schmidt
John Schmidt - avatar
+ 1
Oh I get it now. Thank you very much
25th Dec 2017, 6:44 PM
Marty
Marty - avatar