+ 6
Why is 150?
int x = 100; int y = 200; while(++x < --y){} system.out.print(x);
7 ответов
+ 21
loop will run 49 times ... ie 149 <151 [true]
//after 49th time ... when it goes to check condition for running it for 50th time ...ie ++149 <--151 or 150 <150 [false]
//so x & y will both become 150
+ 7
while loop continues to iterate until the condition becomes false.Here the x and y are pre-incremented and pre-decremented respectively.So
1st iteration: 101<199 (true)
102<198 (true)
................
149<151(true)
150<150(false)
So when condition becomes false, the while loop will stop iterating printing the current value of x which is 150.
you can also try:
System.out.println(y); //outputs 150
+ 7
Note that post increment and post decrement will produce different result.
while(x++ < y--)
System.out.println(x) //outputs 151
System.out.println(y) //outputs 149
Why?
post increment first use the value then updates after.
1st Iteration :100<200 (true)
101<199 (true)
.........
150<150(false)
now when printing it updates value as
x=151 and y=149
Try with different combination in playground and analyze it will help you understand.
+ 5
understood. with sololearn playground the ouputs is 150... thanks so much.
+ 4
thanks everyone. but when i run the program in ms-dos the ouputs in X is 149 :s
+ 3
What you have done is set a while loop that doesn't stop until the value of x is greater than or equal to that of y. What is does is increment x while simultaneously decrementing y, which therefore results in x being equal to 150 (as with y).
Hope this cleared that up! d:
+ 3
pre increment and post increment result discrepancy in ms-dos ...try with online compilers if you do not have IDE..Can't you try solo learn playground?