• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
even the semi-daft ones like me
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I found some pretty shells, some sea glass and this lovely tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic