0

I have this simple code sample that I don't understand.

// Compute integer powers of 2.

class Power {
    public static void main (String args[]) {
        int e, 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);
        }
    }
}

Producing this output.

2 to the 0 power is 1
2 to the 1 power is 2
2 to the 2 power is 4
2 to the 3 power is 8
2 to the 4 power is 16
2 to the 5 power is 32
2 to the 6 power is 64
2 to the 7 power is 128
2 to the 8 power is 256
2 to the 9 power is 512

Questions:

  1. How come result is not always 2, since it's being re-initialized every time the for loop is entered?

  2. The e-- decrement doesn't do a thing either, does it? Since, again, e is being set equal to i afresh on every iteration.

Thanks.

2
  • 2
    Have you tried sitting down with pencil and paper and executing the code yourself? Commented Mar 3, 2016 at 17:26
  • e and result are not being reinitialized. You seem to have a misunderstanding about scope and how for loops work. But, e and result are both initialized outside the loop body. Then they are changed within the loop. Try and step through it with pencil and paper to get a better understanding. Commented Mar 3, 2016 at 17:27

3 Answers 3

1

How come result is not always 2, since it's being re-initialized every time the for loop is entered?

Yes, it is being re-initialized but only in the first loop. Your inner loop is looping while(e > 0) and double result at each iteration. Then once you are done looping, you pring the result and restart. The value of result will depend on e which define the number of times result is doubled.

The e-- decrement doesn't do a thing either, does it? Since, again, e is being set equal to i afresh on every iteration.

Again, yes it is being set back to i at each iteration but that doesn't mean it is useless. At each iteration, e is set back to the new value of i, and then you use it to create an inner loop while e > 0 where at each iteration you decrement e of 1 and double the result.

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

1 Comment

Oh, I get it!!! So the result IS always (2), except that when your 'e' is say 4, it's (1x2)(1x2)(1x2)(1x2) to give 2^4, at each iteration decrementing "e" so that when while evals to false (e == 0) it breaks out of the while and returns to the top of the for. Thanks.
1

The two questions kind of go together.

You see, e is set to i which is actually INCREASED each iteration. And the higher e is, the more often the inner while will be worked through.

So for example in your 3rd for iteration

i = 2
e = 2
result = 1

So first while:

result = result*2 = 1*2 = 2
e = 1

e is still > 0 so after the second while:

result = result*2 = 2*2 = 4
e = 0

And there we go.

e-- did something twice and result is NOT 2.

Comments

0

result and e are re-initialized at the top of the for loop, but are modified within the while loop before result is displayed at the bottom of 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.