+ 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);
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)
+ 2
1) 0+1=1
2) 1+2=3
3) 3+10=13
4) 13+4=17
+ 1
1+2+10+4