Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

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

Curtis Brown
Greenhorn
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?

^Curtis

Sheriff
Posts: 14691
16
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)

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

Virag Saksena
Ranch Hand
Posts: 71
x +=x means x = x + x
which means x = 2*x

I guess what you wanted was x += 1

Curtis Brown
Greenhorn
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
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!

Layne

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

Curtis Brown
Greenhorn
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!
^Curtis