+ 1

Would you tell me why this output is 2?

int x = 0; for (int i=10; i>0;i=i-x){ x++; if(i%2==1){ break; } } System.out.print(x);

9th Jul 2018, 11:51 PM
Afresia
Afresia - avatar
2 Answers
+ 7
i starts as 10. x becomes 1. Next loop, i is 9 and x becomes 2. 9%2 is 9-8=1 so we break out of the loop. We print x of 2.
10th Jul 2018, 12:17 AM
John Wells
John Wells - avatar
+ 1
oo, I got it, thank you John Wells
10th Jul 2018, 12:22 AM
Afresia
Afresia - avatar