While loop condition in java -


i working way through "java - beginners guide herbert schildt". hope question not way ridiculous. while loop condition, while loop located inside loop. code example one:

public static void main(string[] args) {      int e;     int result;      (int = 0; < 10; i++) {          result = 1;         e = i;          while (e > 0) {              result *= 2;             e--;             }         system.out.println("2 " + + " power " + result);         } } 

i don't understand decrementing of e, within scope written after while (e > 0). since e = i, , incremented, believe e should larger 0 after loop has been performed first time. if e not decremented, program not run properly. why decrementing e necessary in example?

what they're doing there each iteration of i, they're multiplying result 2, times.

the walkthrough

i = 0, e = 0, result not multiplied, output 2 power of 0 1

i = 1, e = 1, result multiplied 1 time 2, 2 power of 1 2

etc

they're decrementing e there "count backwards" down 0. e reset each time, , enter while loop on iterations after first one, , exit while loop once counts down zero.


Comments