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);
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.
+ 1
https://code.sololearn.com/c8AIki2yMIWH/?ref=app..
If u want check this..
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...