0

Can someone explain why this loop is not printing from 1 to 10?

I think maybe I assigned the value of x = 1; and not 0, so it skipped 10? 👇 public class Program { public static void main(String[] args) { int x = 1; while ( x < 10) { System.out.println(x++); } } }

27th Sep 2019, 10:36 PM
DevMoralist
3 ответов
+ 3
When x=9, you print 9 and then you increase x by 1 and assign 10 to variable x. Then you check your condition (10<10) which is false and while loop will stop executing. If you want to print 10 then use <= (less or equal) operator or use prefix ++x.
27th Sep 2019, 10:46 PM
voja
voja - avatar
+ 1
Make (while X<=10)
27th Sep 2019, 11:08 PM
Sunday Iliya
Sunday Iliya - avatar
0
Make while (x<=10) Because if you write x<10 that mean when x=10 you will out the boucle because the condition x<10 Is not true, good luck
30th Sep 2019, 9:32 PM
Ghada Ben Khalifa
Ghada Ben Khalifa - avatar