Learn to Program, by Chris Pine

Dave wrote:

JB wrote:

I too wish the author had put examples, say on some pages in the back or
something.

But if you ask this group, you can get interactive answers!

Let's start again with that one. We need a loop counting down from 99. So
first, we need a counter.

beer = 99

Now, we need to count it down.

beer = beer - 1

But we need to do that repeatedly - it goes in a loop.

beer = 99
while (something to end the loop?)
beer = beer - 1
end

How long should we keep looping? We don't want to go past zero. In other
words, we loop while our counter is zero or more.

beer = 99
while beer >= 0
beer = beer - 1
end

OK, now we've got the basic structure of the program - one loop, beer starts
at 99, and decreases until it gets to 0. But there's no output yet.

See how you go putting some flesh on that skeleton, JB - post back when
you're done.

  Hi Dave,

  Okay, here's what I got, and it works too, heh. The 'if' had to be in there,
or else it kept ending with negative numbers (usually -2). This way is as
good as Bill's, but a lot shorter (not putting you down or anything Bill!).

beer = 99
while beer >= 0
    beer = beer - 1
        if (beer - 1) >= 0
        puts beer.to_s + ' ' 'bottles of beer on the wall,' +
             beer.to_s + ' bottles of beer,' +
             'take one down, pass it around,' +
             (beer - 1).to_s + ' ' + 'bottles of beer on the wall!'
        end
end

  Now, to clean it up even more...when it gets down to '1 bottle', I don't
want it to say '1 bottles...' (plural). How would I do this? I tried with an
'else' in there, but for some reason I either made it an infinite loop or it
kept getting the negative numbers all over again.
  I tried with an 'else (beer - 1) == 1' (no quotes of course and that didn't
work either.

  I do appreciate all the help you guys are giving, and yes, the interactive
way will make me work a little harder too to get things right, plus I'm the
kind who *needs* that kind of help or I fail miserably and give up, heh.

JB wrote:

Okay, here's what I got, and it works too, heh. The 'if' had to be in
there,
or else it kept ending with negative numbers (usually -2). This way is as
good as Bill's, but a lot shorter (not putting you down or anything
Bill!).

beer = 99
while beer >= 0
   beer = beer - 1
       if (beer - 1) >= 0
       puts beer.to_s + ' ' 'bottles of beer on the wall,' +
            beer.to_s + ' bottles of beer,' +
            'take one down, pass it around,' +
            (beer - 1).to_s + ' ' + 'bottles of beer on the wall!'
       end
end

Like I told Jan, put the "beer = beer - 1" at the end of the loop - then you
won't need that "if". By the way, that "if" covers the whole loop. Wouldn't
it be simpler just to exit the loop instead of going through again but doing
nothing? Here are the two alternatives I'm suggesting:

beer = 100 # have to add 1, because the first thing we're doing is
subtracting 1!
while (beer - 1) >= 0 # a little better - no need for the inside "if"
  beer = beer - 1
  puts ...
end

beer = 99
while beer >= 0
  puts ...
  beer = beer - 1
end

Also, check out the typo here:

       puts beer.to_s + ' ' 'bottles of beer on the wall,' +

' ' 'bottles...' is the same as ' ' + 'bottles...'. You should change it to
' bottles...'.

Now, to clean it up even more...when it gets down to '1 bottle', I don't
want it to say '1 bottles...' (plural). How would I do this? I tried with
an
'else' in there, but for some reason I either made it an infinite loop or
it
kept getting the negative numbers all over again.
I tried with an 'else (beer - 1) == 1' (no quotes of course and that
didn't
work either.

Bill's solution did this! You might want to read it and figure out how.
Still, I would prefer to do it a different way:

beer = 99
s = ''
while beer >= 0
  if beer == 1
    s = ''
  else
    s = 's'
  end
  puts ... + 'bottle' + s + ...
  beer = beer - 1
end

I do appreciate all the help you guys are giving, and yes, the
interactive
way will make me work a little harder too to get things right, plus I'm
the
kind who *needs* that kind of help or I fail miserably and give up, heh.

We'll keep you accountable. Don't give up.

Cheers,
Dave