2

I didn't understand why in this code that calculates the powers of 2, the e is decremented inside the while loop:

public class Power {
    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);   
        }
    }
}

If I run without the e-- the code only executes the first one. It only works with the e--.

6
  • This is the perfect case for an IDE debugger like the one in Eclipse for instance. Commented Apr 2, 2016 at 15:24
  • 1
    Can you work it out by hand, at least for the first few values of i? Commented Apr 2, 2016 at 15:25
  • 1
    i=0, while loop is false, result =1; i=1, while loop true: 1*2 but then why e--? The i will return to 0 then? Commented Apr 2, 2016 at 15:28
  • 1
    I indented your code correctly. Once indented correctly, it's suddenly much easier to understand the code. Commented Apr 2, 2016 at 15:29
  • 1
    Now I get it, it's only for the while loop, like when the i=2 then e=2, so the while to stop need to be equal to 0, then it will run once and decrement, run again and decrement until it get 0 and stops, and so on. Thanks! Commented Apr 2, 2016 at 15:35

5 Answers 5

3

To understand what is going on, consider the inner loop in isolation from its outer loop:

e = someNUmber;
while (e > 0) {
    result *= 2;
    e--;
}

The loop stops when e reaches zero. That's why there needs to be some operation inside the loop body to change the value of e to get it closer to zero.

Your loop is equivalent to this for loop:

for (int e = someNumber ; e > 0 ; e--) {
    result *= 2;
}

The value of e is your loop counter. It controls the number of iterations of the inner loop, ensuring that it executes exactly i times.

Sign up to request clarification or add additional context in comments.

Comments

1

It is because to calculate the power of 2 to i then you have to multiply 2, i times. So each time i(the required power of 2) is assigned to e and is decremented so that you multiply by i times.

Comments

1

If you look at what happens for the first few values of i, it becomes clear:

i = 0

result = 1; // e = 0, result = 1

i = 1

result = 1;  // e = 1
result *= 2; // e = 0, result = 2

i = 2

result = 1;  // e = 2
result *= 2; // e = 1, result = 2
result *= 2; // e = 0, result = 4

i = 3

result = 1;  // e = 3
result *= 2; // e = 2, result = 2
result *= 2; // e = 1, result = 4
result *= 2; // e = 0, result = 8

The inner loop is ensuring that the result gets multiplied by the number 2 i times.

An (arguably clearer) alternative for the inner loop and the extra e variable would be:

for (int j = 0; j < i; j++) {
    result *= 2;
}

Comments

0

If I run without the e-- the code only executes the first one.

Actually in this case your program stucks in infinite loop

while (e > 0) {
    result *= 2;
}

You see only one result because on first iteration of for loop e equals 0 so program doesn't enter into while loop

Comments

0

e-- is existed to prevent an infinite loop, removing it will result in infinite while loop in the second iteration of for loop since e will never change and thus it'll never become zero, and you'll never break out of the loop to reach the System.out.println statement or to even to go the next iteration in the for loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.