+ 1

java

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); here's code i need a explanation for this

16th May 2020, 12:47 PM
Jass Ahuja
Jass Ahuja - avatar
3 Respostas
+ 3
Its so easy
16th May 2020, 12:48 PM
Shahghasi Adil
Shahghasi Adil - avatar
+ 3
Ans is 17...if i value is 3 then add 10 to prev result that is from 0 to 2 = 3+10+4 (for i = 4).
16th May 2020, 1:03 PM
Pratik Shah
+ 1
Jass Ahuja in loop, all i values are adding to result, except when i=3 so, when i=3, adding 10 to result result = 0+1+2+ 10 +4 =17 When i=5,i<5 fails so loop stops there.. And print the result.
16th May 2020, 1:43 PM
Jayakrishna 🇮🇳