0

Can someone explain this solution for me?

int i = 1; for(; i<5; i++) { i*=i; } system.out.print(i); The solution is 5 but I'm not figuring out how it's arrived there. Thanks!

15th Jun 2017, 4:38 AM
Josh Miller
Josh Miller - avatar
3 ответов
+ 9
put a system.out.print(i) inside the loop after the multiplication. this may help you understand what is going on
15th Jun 2017, 4:47 AM
jay
jay - avatar
+ 3
Did you have deleted my answer? I'm sure to have already posted here ^^ > start: i == 1 > entering 'for' loop, no initialization, always i == 1 > i *= i <=> i = i*i so i == 1 > i is incremented, so i == 2 > i is < 5, so loop again > i = 2*2 == 4 > i is incremented, so i == 5 > i is not < 5, so exit loop > print(i) => 5 (and I had always the text pasted in clipboard :P)
15th Jun 2017, 12:26 PM
visph
visph - avatar
0
Thanks guys!
15th Jun 2017, 5:37 PM
Josh Miller
Josh Miller - avatar