Java number of results generated

1 to 10 , if (x > 3) match for [4,5,6,7,8,9,10]

The probleme here it’s probably where you increment your x, try to put it at the end, you will get 6 results.

public static void main(String[] args) {
int x = 1;
while (x < 10) {

    if (x > 3) {
        System.out.println("Wielkie X");
    }
    x = x + 1;

}

}

To be more precise, when you start your while loop with x = 3, first you’re incrementing x (x = 4), so you print your text despite your started your loop with x = 3.
And so on until 9, at 9 you start your loop, increment x (x = 10) pass your condition (x > 3) and print. It was your last loop, x value is now 10. So you print from 3 to 10, easy math 10-3 = 7

Leave a Comment