File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes unexpected results with a while counter.... (Head First, pg. 13) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "unexpected results with a while counter.... (Head First, pg. 13)" Watch "unexpected results with a while counter.... (Head First, pg. 13)" New topic

unexpected results with a while counter.... (Head First, pg. 13)

Curtis Brown

Joined: Jan 10, 2006
Posts: 20
Hello to all, my first post, but it seems like maybe it's a skosh beyond "beginner" so I'm dropping it in here...

Background, I just got Head First Java (2nd ed.) and was looking over the "sharpen your pencil" on pg. 13.

I thought I'd get a little tricky, and the first time I keyed in the code I used "x += x;" (line 7, which was "x = x + 1;" in the book) and was interested and surprised to find that the output skipped the final "Do" (output was only DooBeeDooBee) until I set the if statement to "x == 4".

Maybe it's only because it's getting late that I'm missing the cause, but from how I'm looking at this, I don't see how x ever got to 4... if it get's incremented to 3, then the while loop should end and not increment x again, yet somehow x gets to 4...

Is there a simple explenation for this?

Thanks in advance!
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

x += x; is different from x = x + 1 !!!

Just start from 1
x += x; -> x += 1 -> x = 2
x += x; -> x += 2 -> x = 4 (the loop breaks here, x = 4)

x = x + 1 -> x = 1 + 1 -> 2
x = x + 1 -> x = 2 + 1 -> 3 (the loop breaks here, x = 3)

[My Blog]
All roads lead to JavaRanch
Virag Saksena
Ranch Hand

Joined: Nov 27, 2005
Posts: 71
x +=x means x = x + x
which means x = 2*x

I guess what you wanted was x += 1

<a href="" target="_blank" rel="nofollow">The Peak of Performance</a>
Curtis Brown

Joined: Jan 10, 2006
Posts: 20
Ahh, yes! Now I feel silly! Thanks for the quick replies!

x += 1

That would have definately been the behaviour I was trying for....
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Notice how the behavior was described by those that answered here. I'm just pointing this out so you can use a similar technique in the future. Usually a table of some kind that contains the values of variables can be very helpful in understanding what your code is doing. Pull out a pencil and piece of paper to do this. Sure it may seem antiquated in this electronic age, but it sure helps in many situations. As you fill out the table, mentally execute each line of code and calculate what the values should be for each variable. Of course, this means you need to understand what each line actually does. But that's part of the learning process.

Welcome to the Ranch, Curtis!


p.s. If you are only on page 13 in HFJ, then you should probably post future questions to the beginner's forum.

Java API Documentation
The Java Tutorial
Curtis Brown

Joined: Jan 10, 2006
Posts: 20
Thanks Layne.

Thank you for the welcome!

I'm getting used to that "using scratch paper" idea.... (I found it absolutely requred to finish the Mixed Messages task on pg. 23) And, as of this moment, I'm up to pg. 60!

I was going to post the question in the beginner forum originally, but then I saw that caveat:

BEWARE: Persons caught posting questions of any difficulty in this forum are subject to fines of up to 10 post responses.

And it frightened me... I thought maybe the bar was *really* low for that forum

Anyway, I'll take your advice and stay there a bit before venturing back to intermediateville!

Thanks once again to the helpful, friendly, and timely responces!
I agree. Here's the link:
subject: unexpected results with a while counter.... (Head First, pg. 13)
It's not a secret anymore!