Keith Engwall

Greenhorn
+ Follow
since Dec 02, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Keith Engwall

Campbell Ritchie wrote:Welcome to the Ranch

You have a test about whether beerNum > 0 inside a while (beerNum > 0) loop. It is much neater to move the "no more bottles" line outside the loop, then you don't need that test any more. You are duplicating a test with the change from "bottles" to "bottle", but that probably still fits better inside the loop.
By the way: your indentation is inconsistent. Sometimes you have { at the end of the line and sometimes on a line to itself. I prefer the line to itself convention, but that is a preference not a requirement. You should however be consistent about which convention you follow.



I tried what you suggested and I found that if you take out the test about whether beerNum > 0 , then it will print "0 bottle of beer on the wall" before it exits the while loop and prints "No more bottles of beer on the wall". That's not correct.

In my opinion, that if/else test is the most elegant way to represent the decision of what to print as the last line of each stanza... if it's any stanza but the last, print the number of bottles of beer left, but if it's the last stanza, print "No more bottles". Comments would help clarify the meaning, of course.



There may be a more efficient way in terms of computer cycles to handle the situation, but sometimes I find it's better to invest those cycles in making the code make more sense to someone down the line (often my future self) who has to figure out what I was trying to do.

And as for the original problem with bottles vs. bottle when beerNum is reduced to 1, I agree that setting word to "bottle" after beerNum is reduced to 1 makes a great deal of sense.

Here's my "final" code... I added punctuation to the first line, dividers between stanzas, and draw out the last line the way we used to do on those long car trips.



If you don't like my comments, blame the beer! ;-D
12 years ago