Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Answer emphasizes of a different aspect of loop - sybex 829

 
Greenhorn
Posts: 13
2
  • 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: 41184
848
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
Greenhorn
Posts: 13
2
  • 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.
 
Ranch Foreman
Posts: 25
1
  • 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: 41184
848
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
Greenhorn
Posts: 13
2
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic