int i = 3, j = 3;
for (; i++ == j--; i += 2, j -= 2) {
do {
i = i + j;
} while (i % j != 0);
}
System.out.println(i);
System.out.println(j);
I tried debugging it in Eclipse, and here’s the result:
- i,j
- 3,3
- 4,2
- 6,2
- 9,-1
Since the last time for loop checked the values of i and j, they were not equal to each other, why did it came out of the loop? Wouldn’t it be an infinite loop?
Condition
++i != --jori++ != --jor++i != j--will cause infinite loop.i++ and j– both are post increment and decrements respectively so first it will check the condition and later increment the value.