Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chapter 2 - The for statement 2. Adding Multiple Terms to the for statement Java OCA 8,Sybex

 
Tom Keegan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I'm using the electronic version so I don't have page numbers to refer to.

In chapter 2, under the section "The for Statement", example "2. Adding Multiple Terms to the for Statement



the output the book says I should get is:
0 1 2 3 4

the output I get is:
0 1 2 3 4 5

I've triple checked the code I'm compiling and I'm sure I haven't entered a typo, and the output I'm getting makes sense to me, here's my reasoning:

at the end of the fifth iteration x=4, y=4
so on the next iteration (sixth) the boolean expression evaluates to true, and x and y are each incremented to 5
boolean expression now evaluates to false, so there is no seventh iteration
at the end of the for loop, printed out is "0 1 2 3 4"
after the for loop you have the print statement that prints out the value of x, which is 5, so you have
"0 1 2 3 4 5"

Where is my logic going wrong? Or have I indeed typed something incorrectly.

Thankye all kindly
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom,
You typed and understood it correctly. In the print book, this is on page 82 and logged in the errata as "In the first example on the page, System.out.print(x) should be removed". That's where the stray 5 is coming from. The line that shouldn't be there.

Thanks for asking rather than staying confused!
 
Tom Keegan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help Jeanne, and sorry, I did check the errata, but clearly not thoroughly. I'll be sure to go over the errata in depth before posting another question.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you could have figured out it was the same thing without knowing the page number in the printed book.

I wasn't criticizing; just explaining why I didn't add it to the errata after your thread ;).
 
Tom Keegan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne

You're uber awesome for taking the time out for your readers
 
Tom Keegan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even the semi-daft ones like me
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. And don't worry. The daft ones are quietly confused and not learning. You are smart for asking!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:And don't worry. The daft ones are quietly confused and not learning. You are smart for asking!

And the very smart ones, use each code snippet to do some experimenting and twiddling to learn even more

So let's give you a hand and create a bunch of examples with some minor changes to the original code snippet. And the question each time is the same: what's the output? Will it be different than the original one? Here we go!

Example 1: use bitwise & operatorExample 2: use postfix increment operator in condition expressionExample 3: use prefix increment operator in condition expressionExample 4: use conditional || operator and prefix increment operatorExample 5: use bitwise | operator (and prefix increment operator)Example 6: use conditional || operator and postfix increment operatorExample 7: use bitwise | operator (and postfix increment operator)

So much fun with such a small code snippet!

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic