• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Bear Bibeault
  • paul wheaton
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Mikalai Zaikin
  • Piet Souris
Bartenders:

Sybex OCP 11 Programmer I. Chapter 4. Answer to review question 13

 
Ranch Hand
Posts: 53
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jeanne and Scott, I have found a possible typo in answers for review question 13. Here answer states that three distinct numbers are printed, it is not true. Three numbers printed, two of them distinct.

B, E. The code compiles without issue and prints three distinct numbers at runtime,


Excerpt From: Boyarsky, Jeanne. “OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide”.
 
Saloon Keeper
Posts: 3848
42
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Oksana !
Could you post more context?
Jeanne and Scott are members here, but might reply not quickly, and there are many other people on this forum who can help/confirm who have no the book.
If you share more context, you might get an confirmation faster.
Best regards,
MZ
 
Oksana Cherniavskaia
Ranch Hand
Posts: 53
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it really not a question on the subject. This is a clear typo. The context is that in program 3 figures printed, but both of them the same. So there are no 3 distinct numbers. That is all.
 
author & internet detective
Posts: 41763
885
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Confirmed and added to the errata. We even list two correct answers for those two distinct numbers!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm not sure if the explaination

"In the second loop, animals already starts out not less than or equal to 1, but since it is a
do/while loop, it executes at least once."

for the code snippet



is intended as I would read it.

To me it sound as if in a similiar while-loop the animals++ in the boolean expression wouldn't be executed. But as it is a part of the boolean expression and not of the loop's (empty) body, it should be executed here, too.
 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!
The it in question is the contents of the
{}

that is, the body of the loop.

In previous exchanges with the authors, which can be found here, there were a number of times that they acknowledged that some other wording could perhaps be slightly clearer but was simply too verbose.  This might be another of them.

The difference between a while() { } loop and a do { } while () loop is that THE BODY of a do { } while loop will always execute at least once.
The conditional expression in a while() loop will always be evaluated at least once if the execution ever reaches the line containing the while()

So the conditional expression in a do { } while() loop will always be evaluated at least once, unless the code in the { } calls System.exit() or throws an exception which is not caught within the loop body.  In those cases it will never be evaluated.
Or, if you are a goofball and do something like this:
jshell> do { System.out.println("ha ha ha!"); break;} while (1 < 10)
ha ha ha!


I tend to agree with the authors that being this verbose in the book would just take up too much space, it is fun at the end of a work day to discuss it tho.
 
R. Klee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"In the second loop, animals already starts out not less than or equal to 1, but since animals++ is part of the boolean expression, it is executed once."
wouldn't be more verbose and it would make clear that the execution of the loop's body isn't the point here.
I don't think it needs an full explaination, too, as it is already explained in the chapter.
 
Jesse Silverman
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think in that place they are drawing the essential distinction between the while loop and the do while loop.
In both cases (except the wacky ones I showed for fun) the boolean expression is always evaluated at least once, that is what they have in common.
The essential difference between the while and do while loops is that the body of do while always gets executed at least once for all cases, whereas zero executions of the body is a perfectly expected outcome of a while loop.

I will leave it to the authors to consider, it wouldn't make an errata but I believe they are taking a hard look at things that might be made clearer in the next book covering Java 17/829 exam.  
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic