class ExcerciseLib1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 1;
while (x < 10) {
x = x + 1;
if (x > 3) {
System.out.println("big x");
}
if (x < 4) {
System.out.println("big 1");
}
}
}
}
In the code above, big 1 get's output 2 times, and big x gets output 7 times. I don't quite understand why big x outputs 7 times, shouldn't it be 6 times?
I get that
if (x < 4) {
System.out.println("big 1");
}
outputs "big 1" twice because using the loop,
- x = 1 + 1 equals 2 which is less than 4,
- x = 2 + 1 equals 3 which is less than 4.
So for
if (x > 3) {
System.out.println("big x");
}
Shouldn't the loop only output it 6 times?
- x = 3 + 1 equals 4, 4 is greater then 3, so output big x.
- x = 4 + 1 equals 5, 5 is greater than 3, so output big x.
- x = 5 + 1 equals 6, 6 is greater than 3, so output big x.
- x = 6 + 1 equals 7, 7 is greater than 3, so output big x.
- x = 7 + 1 equals 8, 8 is greater than 3, so output big x.
- x = 8 + 1 equals 9, 9 is greater than 3, so output big x.
- x = 9 + 1 equals 10; but the loop states x < 10
I'm getting brain stumped on this one. Am I understanding the "x = x + 1" expression wrong?
System.out.println(x+",big x");you'll get answer.