0
Can someone explain how this for loop works in conjunction with this variable?
This is the 2nd question in the 3rd module of java. when i run the code, it gives me 17. While i could run with this answer, i really need to understand how it got to that number. thanks in advance. int result = 0; for (int i = 0; i < 5; i++) { if (i == 3) { result += 10; } else { result += i; } } System.out.println(result);
1 Resposta
+ 1
Here is the breakdown:
result = 0
1st loop: i =0, result = 0+0 = 0
2nd loop: i = 1, result = 0+1 = 1
3rd loop: i = 2, result = 1+2 = 3
4th loop: i = 3, result = 3+10 = 13
5th loop: i = 4, result = 13+4 = 17
i=5, exit from loop
print result which is 17