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

Answer emphasizes of a different aspect of loop - sybex 829

 
Ranch Hand
Posts: 67
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

With reference to the answer to question 17 in chapter 3.  I got the impression that the value of the variable animals ended up 3 because the loop used is a do/while loop (see attached image).  This variable would have had the same result if a while loop was used instead.

While technically correct (that the body of the do/while loop is always executes at least once), do you think that the answer should emphasis the point that the body of the loop has nothing to do with the outcome?
Answer.png
Answer to question 17 in chapter 3
Answer to question 17 in chapter 3
 
author & internet detective
Posts: 41775
887
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
If it was a while loop, the body would not execute. Animals starts out as 2. Then we have:


Granted there is no contents in the loop body. But the empty body does execute.
 
Albert Attard
Ranch Hand
Posts: 67
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply, Ms. Boyarsky.

I didn't understand your reply and the following may be based on my wrong understanding.

Am I correct to understand that if the do/while loop in this example is switch to a while loop, we would have a different result?

While it is correct that the body of the do/while is always executed once, this does not play any role in the question's outcome, as the variable animals is incremented within the loop's condition check.   Switching the loop to a while loop will produce the same output.

do/while loop (original, as shown in the book)


while loop


Both examples yield the same output as the animals variable is incremented within the loop's condition check, which is always executed.  The difference in this case is that in the do/while loop, the condition is evaluated after the loop's body is executed and in the case of the while loop, the condition is executed before the loop's body.  Both examples yield the same output.



Because of this, I would emphasis that the do/while body loop has nothing to do with the output, and the while loop would have produced the same result.

Thank you once more for taking the time to reply.
 
Greenhorn
Posts: 25
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Albert,
I think the book is not saying that the value of "animals" is 3 for the fact that this is a do/while loop, but it's just remembering to the student how a do/while loop works.
 
Jeanne Boyarsky
author & internet detective
Posts: 41775
887
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
What Marco said. I agree the result would be the same because the loop body is empty.
 
Albert Attard
Ranch Hand
Posts: 67
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your replies, Marco and Jeanne.

Maybe I am reading too much into this.  The answer to this question contains the following:

…, but since it is a do/while loop, it executes at least once.



I believe that this part of the answer can be omitted (or rephrased) as it is emphasizing a point that has not impact on this question.
 
Jeanne Boyarsky
author & internet detective
Posts: 41775
887
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

Albert Attard wrote:I believe that this part of the answer can be omitted (or rephrased) as it is emphasizing a point that has not impact on this question.


That's ok though. Sometimes things are mentioned in passing.
 
Stinging nettles are edible. But I really want to see you try to eat this 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