why is the answer for this is 17 i can't really get it , can someone explain for me please ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why is the answer for this is 17 i can't really get it , can someone explain for me 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);

13th Nov 2019, 5:52 PM
Zahra Fati
Zahra Fati - avatar
3 Answers
+ 8
Zahra Fati This code snippet works as below mentioned way. What happening here is first for loop is worked from 0 to 4 at 5 condition false and loop terminate itself and print the result ▶When i = 0, i == 3 is false, result += i, result = 0. ▶When i = 1, i == 3 is false, result += i, result = 1. ▶When i = 2, i == 3 is false, result += i, result = 3. ▶When i = 3, i == 3 is true, result += 10, result = 13. ▶When i = 4, i == 3 is false, result += i, result = 17.
13th Nov 2019, 6:03 PM
GAWEN STEASY
GAWEN STEASY - avatar
13th Nov 2019, 6:03 PM
Sujithra
0
i=0, r=r+i==0+0=0; //else part i=1, r=r+i=0+1=1; //else part i=2, r=r+i=1+2=3; //else part i=3, r=r+10=3+10=13; //in if part i=4, r=r+i=13+4=17 ; //else part Answer prints result 17 here 'r' means result...
13th Nov 2019, 6:03 PM
Jayakrishna 🇮🇳