• Post Reply Bookmark Topic Watch Topic
  • New Topic

While loop question  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
e-- decrements e.

If result=1 I would think it's always 1 *= 2


That's true, but why is that a problem?
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because if result is always 1 *=2 then I don't see how the output for result can be 2, 4, 8, 16, etc. since it's always 1.

I also understand e-- means minus one but I don't see what it's doing or why it matters.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the result always 1 *= 2?
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because result = 1;

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:-
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
Also remind yourself of the workings of the *= operator. That link isn't easy to read, I am afraid.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I thought the outer loop was decreasing. Start from i at 0 and you should have solved it before i reaches 3.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that: 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote:Because result = 1;



Ahh..but you forgot about this:

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume that means you worked it out?

And

You're welcome
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. . . .
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.

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?
     
    Ryan Bishop
    Ranch Hand
    Posts: 143
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, thank you very much. The issue was understanding how the inner loop kept going until a condition was met, then going to the first loop.
    Before I came on I was doing the below and finally realized the structure. I appreciate you being so helpful all the time.

     
    Campbell Ritchie
    Marshal
    Posts: 56584
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!