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!
3 Respuestas
+ 9
put a system.out.print(i) inside the loop after the multiplication. this may help you understand what is going on
+ 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)
0
Thanks guys!