0

What????

This has been bugging me for a while now:how the heck did the end result of question two of the quiz at the end of the array section come to be seventeen. It's probably something simple that I'm missing or something but it has been bugging me for a while now I just don't understand. int result = 0; for (int i = 0; i < 5; i++) { if (i == 3) { result += 10; } else { result += i; } } System.out.println(result); Also to whoever can explain this to me thank you. :)))

23rd Nov 2016, 5:17 AM
Victor
Victor - avatar
3 Antworten
+ 1
1. i=0: result=0+0=0 2. i=1: res=0+1=1 3. i=2: res=1+2=3 4. i=3: res=3+10=13 5. i=4: res=13+4=17
23rd Nov 2016, 5:29 AM
Rill Chritty
Rill Chritty - avatar
+ 1
int result = 0; for (int i = 0; i < 5; i++) { //loop runs from 0 to 4. if (i == 3) { //passes only for 3 result += 10; // adds 10 for 3 } else { result += i; //adds 0, 1, 2, 4 } } System.out.println(result); So, it is 0+1+2+4+10 == 17.
23rd Nov 2016, 5:39 AM
Nagendra Prajwal
Nagendra Prajwal - avatar
+ 1
ohh wooww........ I knew it was going to be something easy like that yet it stumped me for like 20-60 min lol. Thank you though for explaining I greatly appreciate it. I feel so dumb lol.
23rd Nov 2016, 6:14 AM
Victor
Victor - avatar