I am not able to understand ...the result is 17 ..but why? can any explain me😥😥 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I am not able to understand ...the result is 17 ..but why? can any explain me😥😥

What is the output of this code? int result = 0; for (int i = 0; i < 5; i++) { if (i == 3) { result += 10; } else { result += i; } } System.out.println(result);

17th Sep 2017, 12:29 PM
Sonu Kumar Jha
Sonu Kumar Jha - avatar
3 Answers
+ 13
So at the start 'result' is equal to 0, then for i equals 0 'result += i' (which is result = result + i, which is 0 + 0), then i is 1 so 'result += i' makes result 1, then for i = 2, result = result + 2 = 1 + 2 = 3, then for i = 3, result = result + 10 = 3 + 10 = 13, then for i = 4, result = result + 4 = 13 + 4 = 17, and then i has to less than 5 for the loop to work i = 5 is invalid so the loop is broken out of and the value if result is printed (which is 17)
17th Sep 2017, 12:38 PM
LynTon
LynTon - avatar
+ 2
1) 0+1=1 2) 1+2=3 3) 3+10=13 4) 13+4=17
17th Sep 2017, 12:42 PM
Anton
Anton - avatar
+ 1
1+2+10+4
17th Sep 2017, 12:37 PM
Enzo
Enzo - avatar