How is the result 17 please i need good explanation on this, anybody please? 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); | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How is the result 17 please i need good explanation on this, anybody please? 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);

20th Feb 2017, 2:10 PM
keita Abdullah
keita Abdullah  - avatar
7 Answers
+ 7
i=0 <5(true) result=0+0=0 i=1 <5(true) result=0+1=1 i=2 <5(true) result=1+2=3 i=3 <5(true) result=3+10=13 (if i=3) i=4 <5(true) result=13+4=17 i=5 <5(false) Stop loop
20th Feb 2017, 2:19 PM
Didi Georgel Danaila
Didi Georgel Danaila - avatar
+ 7
1. 0+0=0 // when i = 0 add 0 2. 0+1=1 // when i =1 add 1 3. 1+2=3 // when i = 2 add 2 4. 3+10=13 // when i = 3 add 10 5. 13+4=17 // when i = 4 add 4 6 . 5 is not less than 5 Hence answer is 17
20th Feb 2017, 2:24 PM
Akshata
Akshata - avatar
+ 5
@didi in second last line update 13+4=17
20th Feb 2017, 2:23 PM
Sizr
Sizr - avatar
+ 3
Thank you all I really appreciate
20th Feb 2017, 2:27 PM
keita Abdullah
keita Abdullah  - avatar
+ 2
thx @Sizr
20th Feb 2017, 2:25 PM
Didi Georgel Danaila
Didi Georgel Danaila - avatar
0
thx guyz.
20th Feb 2017, 6:39 PM
Ash Chinnapen
Ash Chinnapen - avatar
0
befor i==3 it executing else condition and when i became 3 , it will add 10 after that again continue with else .
9th Mar 2017, 11:03 AM
Sani Suryavanshi
Sani Suryavanshi - avatar