Ryan Bishop

Ranch Hand

Posts: 143

5

posted 3 years ago

Hi,

This is directly from Java: A Beginner's Guide by Herbert Schildt (page 82). Below is the code and output. I've spent a long time looking

at this and I just don't understand what is going on. If result=1 I would think it's always 1 *= 2 and

I have no clue what the e-- does. I don't understand how this does 2 to the various powers.

output...

This is directly from Java: A Beginner's Guide by Herbert Schildt (page 82). Below is the code and output. I've spent a long time looking

at this and I just don't understand what is going on. If result=1 I would think it's always 1 *= 2 and

I have no clue what the e-- does. I don't understand how this does 2 to the various powers.

output...

Ryan Bishop

Ranch Hand

Posts: 143

5

Campbell Ritchie

Marshal

Posts: 56584

172

posted 3 years ago

I suggest you get a pencil and paper and go through the program from start to finish. Well, try starting when the loop has run a few times and e (for exponent) has got down to 2. It will look something like this:-

Also remind yourself of the workings of the *= operator. That link isn't easy to read, I am afraid.1 = 2

e = 2

result = 1

enter inner loop

result = ??

e = 1 still > 0

reenter inner loop

result = ??

e = 0 not > 0

print result

i = ??

etc etc etc

Campbell Ritchie

Marshal

Posts: 56584

172

posted 3 years ago

Sorry, I thought the outer loop was decreasing. Start from i at 0 and you should have solved it before i reaches 3.

posted 3 years ago

Note that:

So, if result = 1, then:

first iteration: result = 1 * 2 = 2

second iteration: result = 2 * 2 = 4

third iteration: result = 4 * 2 = 8

etc.

`result *= 2;`means the same as:`result = result * 2;`- in other words, multiply the current value of result by 2 and store it in result.So, if result = 1, then:

first iteration: result = 1 * 2 = 2

second iteration: result = 2 * 2 = 4

third iteration: result = 4 * 2 = 8

etc.

Campbell Ritchie

Marshal

Posts: 56584

172

Ryan Bishop

Ranch Hand

Posts: 143

5

posted 3 years ago

Nah, not really but didn't want to be a pain. I don't think I'm understanding the for...while loop. It makes perfect sense to me that the result would be cumulative

when it's explained (like 2*2, 4*2, etc.) but I don't understand why it does that within the loop if when it loops back the result is always

equal to 1. Obviously the answer is the result changes but I don't understand the logic of the loop to know why. Meaning, if the result goes from 1 to 2 I don't

understand why the result=1; in the main loop changes to 2 rather then continuing to be 1. It's more I've never seen that whereas I've seen the structure

(i=0; i < 10; i++) a lot to know what to expect. I completely understand the result changes from 1 to 2 to 4 but I don't understand the underlying form

of the loop or why it does that (if that makes sense).

e-- still makes no sense to me and I don't see why it's even needed. My thinking is wrong but in my mind if I see:

(i=4; i < 10; i++)

and e is then equal to the original i (4) or the i++(5), I don't see how in either condition it would make sense to subtract one from it.

I'm ultimately not understanding the underlying logic. It's not even so much this problem as just being aware for when it comes up

with possibly more complex problems in the future.

Ah well, I'll just keep staring at it.

Campbell Ritchie wrote:I presume that means you worked it out?

And

You're welcome

Nah, not really but didn't want to be a pain. I don't think I'm understanding the for...while loop. It makes perfect sense to me that the result would be cumulative

when it's explained (like 2*2, 4*2, etc.) but I don't understand why it does that within the loop if when it loops back the result is always

equal to 1. Obviously the answer is the result changes but I don't understand the logic of the loop to know why. Meaning, if the result goes from 1 to 2 I don't

understand why the result=1; in the main loop changes to 2 rather then continuing to be 1. It's more I've never seen that whereas I've seen the structure

(i=0; i < 10; i++) a lot to know what to expect. I completely understand the result changes from 1 to 2 to 4 but I don't understand the underlying form

of the loop or why it does that (if that makes sense).

e-- still makes no sense to me and I don't see why it's even needed. My thinking is wrong but in my mind if I see:

(i=4; i < 10; i++)

and e is then equal to the original i (4) or the i++(5), I don't see how in either condition it would make sense to subtract one from it.

I'm ultimately not understanding the underlying logic. It's not even so much this problem as just being aware for when it comes up

with possibly more complex problems in the future.

Ah well, I'll just keep staring at it.

Campbell Ritchie

Marshal

Posts: 56584

172

posted 3 years ago

Go through the inner loop: Let's say you start with e=2 and result=1, so you have already run the outer loop twice.

You go into the loop with e=2 and result=1 (hereinafter called r) First pass: r*=2 turns r to 2 and e-- reduces e to 1. e is still > 0 Second pass: r*=2 turns r to 4 and e-- reduces e to 0. e is now 0 so no longer > 0, so you terminate the inner loop. print 2nd power of 2 = 4 Outer loop still running, so back to the line starting “for”. The bit about i < 10 is still true, so we repeat the outer loop. i++ and e = i set both e and i to 3. Now back to the first line starting with a spot, only this time e is 3 and r is again set to 1. Does that help?

- 1

You have two loops inside each other. The outside loop sets result at 1 and e for exponent to a different value, increasing every time.Ryan Bishop wrote: . . . when it loops back the result is always

equal to 1. Obviously the answer is the result changes but I don't understand the logic of the loop to know why. . . .

Go through the inner loop: Let's say you start with e=2 and result=1, so you have already run the outer loop twice.

Ryan Bishop

Ranch Hand

Posts: 143

5