What will the program bring to the console?

public class Test {
    public static void main(String[] args) {
        boolean b = true;
        int x = 0;

        do {
            if (x++ > 5)
                b = false;
            System.out.print(x);
        } while (b);
    }
}
Explanation
It is obvious that the numbers on the screen will be displayed sequentially. It remains only to find out what number will be displayed the first, and witch one the last.
Since at each test the condition of the variable x increases then 1 will be the first number to be printed.
At the last iteration of the condition x++ > 5 must become true. Since the value of the expression x++ is the original value of the variable, it will be at x = 6. But on the screen will be displayed a new larger value, ie, 7.
We'll get the answer: 1234567.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback