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++); } } }
3 RĂ©ponses
+ 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.
+ 1
Make (while X<=10)
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