class Testing {
public static void main(String args[]) {
int e;
int result;
for(int i = 0; i < 10; i++) {
result = 1;
e = i;
while(e > 0) {
result *= 2;
e--;
}
System.out.println("2 to the " + i + " power is " + result);
}
}
The above code tests the integer power of 2 from 0 to 9. I'm confused as to why e--;
is required in the while loop? Removing it causes an infinite loop error, but why? Wouldn't the code automatically break on the 9th time considering e = i
and i
already having a limit to be less than 10?
I would appreciate a simple/beginner explanation.