This may seem silly, but I’m a bit confused about the following code:
public class Loops{
public static void main(String[] args) {
int i = 0;
int j = 2;
int k = 0;
boolean continue = i<j;
while (continue && k < 2) {
i++;
j--;
k++;
}
System.out.println(j);
}
}
This program prints 0, but I just don’t understand why it doesn’t print 1. The way I see it, after one loop i = j = 1. And thus continue = false. So if anyone can explain to me the logic behind this I would greatly appreciate it!
continue does not reevaluate itself after every loop iteration because he is defined outside of the loop.
instead, check in the loop condition for i < j