is there anyOne who can Solve this for me???????? and why 17 ans???? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

is there anyOne who can Solve this for me???????? and why 17 ans????

int result = 0; for (int i = 0; i < 5; i++) { if (i == 3) { result += 10; } else { result += i; } } System.out.println(result);

20th Oct 2017, 4:42 PM
Ishtiaq Hussain
Ishtiaq Hussain - avatar
3 Answers
+ 13
int result = 0; for (int i = 0; i < 5; i++) { if (i == 3) { result += 10; } else { result += i; } } System.out.println(result); Explanation : 1. when I=0 else condition executed result =0 2. when I=1 else condition executed result =0+1 3 when I=2 else condition executed result= 1+2 4. when I=3 if condition executed result= 3+10 5. when I=4 else condition executed result =4+13 //hence result is 17
20th Oct 2017, 4:55 PM
P R
P R - avatar
- 1
i goes like this 0 1 2 3 4 whenever i is 3 its +10 with result else +i so... i=0...result is 0+0 i=1.....result is 0+1 i=2....result is 1+2 i=3....result is 3+10 i=4....result is 13+4--->17
20th Oct 2017, 4:51 PM
sayan chandra
sayan chandra - avatar
- 1
Initially : result = 0 Iteration 0 : result = 0+0 Iteration 1 : result = 0+1 Iteration 2 : result = 1+2 Iteration 3 : result = 3 + 10 = 13 // if i = 3 add 10 // Iteration 4 : result = 13 + 4 = 17
20th Oct 2017, 4:51 PM
Niush
Niush - avatar