+ 1

While Loop with --x

Why does the following code output include 0? public class Program { public static void main(String[] args) { int x = 3; while(x > 0) { System.out.println(--x); } } } Thanks.

16th Sep 2017, 3:55 AM
Michael
1 Antwort
+ 11
--x decrements the value of x before it is used. At the last loop, x is 1. x > 0 is true, System.out.println(--x); // prints 0. Loop ends.
16th Sep 2017, 3:58 AM
Hatsy Rei
Hatsy Rei - avatar