Learning Ruby question regarding Looping

Hey there, I'm new to this forums as well as being new to Ruby. I
decided to slowly learn ruby since I always wanted to learn a
programming language. But I need some help to understand looping.

I'm using Chris Pine's Learn to Program book and I've been playing
around with the exercises, trying to expand the and make them do things
beyond the original programming. But I just don't understand looping.

Pine says that to make a program loop you have to give a 'while'
condition that's always true. And then provide a way to break out of the
loop. My sample program is a teacher asking a student his name and if
his name isn't capitalized then she'll reprimand him. If his reply isn't
"yes" then she'll keep repeating his reply.

···

------------------
puts 'What is your name, young man?'
name = gets.chomp

If name == name.capitalize
   puts 'Please take a seat, ' + name.capitalize + '.'
else
   puts name + '? You mean, ' + name.capitalize + ' , right?'
   puts 'Don\'t you know how to spell your name?'
   reply = gets.chomp

#start loop
    while reply != 'yes'
        puts reply + ' ? What!?'
        If reply == 'yes'
             break
        end
    end
end
-------------

And the result is just the What!? part being repeated over and over
again infinitely. So clearly I don't understand loops. Any insight?

And sorry if this is an obvious mistake. I studied science in school,
not computer science or logic but I'm trying! Thanks for any help you
guys can give! :slight_smile:

--
Posted via http://www.ruby-forum.com/.

'reply' never gets a chance to change.

Try putting your gets for reply inside the while loop at the bottom, so the
student has a chance to change their input.

Try this:

#start loop
while reply != 'yes'
  puts reply + ' ? What!?'
  if reply == 'yes'
    break
  else
    reply = gets.chomp
  end
end

···

On Oct 11, 2011 8:28 AM, "Dennis Nguyen" <zerofiftysix@gmail.com> wrote:

Hey there, I'm new to this forums as well as being new to Ruby. I
decided to slowly learn ruby since I always wanted to learn a
programming language. But I need some help to understand looping.

I'm using Chris Pine's Learn to Program book and I've been playing
around with the exercises, trying to expand the and make them do things
beyond the original programming. But I just don't understand looping.

Pine says that to make a program loop you have to give a 'while'
condition that's always true. And then provide a way to break out of the
loop. My sample program is a teacher asking a student his name and if
his name isn't capitalized then she'll reprimand him. If his reply isn't
"yes" then she'll keep repeating his reply.

------------------
puts 'What is your name, young man?'
name = gets.chomp

If name == name.capitalize
  puts 'Please take a seat, ' + name.capitalize + '.'
else
  puts name + '? You mean, ' + name.capitalize + ' , right?'
  puts 'Don\'t you know how to spell your name?'
  reply = gets.chomp

#start loop
   while reply != 'yes'
       puts reply + ' ? What!?'
       If reply == 'yes'
            break
       end
   end
end
-------------

And the result is just the What!? part being repeated over and over
again infinitely. So clearly I don't understand loops. Any insight?

And sorry if this is an obvious mistake. I studied science in school,
not computer science or logic but I'm trying! Thanks for any help you
guys can give! :slight_smile:

--
Posted via http://www.ruby-forum.com/\.

Yes, that makes sense! Thanks for your help!

···

--
Posted via http://www.ruby-forum.com/.

Try thinking like the computer. Remember, it's wonderfully fast and
has a (nearly) flawless memory... but it does only exactly what you
tell it to do, not what you *want* it to do, or what any human can
clearly see you (almost certainly) meant. Step through the program,
pretending to be the computer. Note the values of the variables, the
results of comparisons, any input or output, etc. Try it with
different input values. Way Back When, we used to call this "hand
simulation", dirty as that may sound. :wink:

In this particular case, assuming the reply wasn't "yes", you'll note
yourself asking "What!?" over and over. Ask yourself why you're doing
that over and over. It's because each time you check the answer, it
isn't "yes" -- and it *keeps on* not being "yes". Ask yourself, how
can I make it *become* "yes"? The answer is, you have to let the user
type it in again. Then the light bulb should turn on over your head,
regarding where the "reply = gets.chomp" needs to be. :slight_smile:

-Dave

···

On Tue, Oct 11, 2011 at 11:27, Dennis Nguyen <zerofiftysix@gmail.com> wrote:

But I need some help to understand looping.

--
LOOKING FOR WORK, preferably Ruby on Rails, in NoVa/DC; see main web site.
Main Web Site: davearonson.com
Programming Blog: codosaur.us
Excellence Blog: dare2xl.com

"if reply == 'yes'" is not necessary because reply is always not equal
to 'yes' inside the loop.

Using while loops is one of the bests ways to make loops because the
test to end the loop is at the beginning and so, you don't need a break
statement. Instead, put all the tests next to 'while' using logicals
operators if needed. You'll get a more readable code.

Try this :

puts 'What is your name, young man?'
name = gets.chomp

if name == name.capitalize
   puts 'Please take a seat, ' + name.capitalize + '.'
else
   puts name + '? You mean, ' + name.capitalize + ' , right?'
   puts 'Don\'t you know how to spell your name?'
   reply = gets.chomp

#start loop
  while reply != 'yes'
    puts reply + ' ? What!?'
    reply = gets.chomp
  end
end

···

'reply' never gets a chance to change.

Try putting your gets for reply inside the while loop at the bottom, so the
student has a chance to change their input.

Try this:

#start loop
while reply != 'yes'
  puts reply + ' ? What!?'
  if reply == 'yes'
    break
  else
    reply = gets.chomp
  end
end
On Oct 11, 2011 8:28 AM, "Dennis Nguyen" <zerofiftysix@gmail.com> wrote:

> Hey there, I'm new to this forums as well as being new to Ruby. I
> decided to slowly learn ruby since I always wanted to learn a
> programming language. But I need some help to understand looping.
>
> I'm using Chris Pine's Learn to Program book and I've been playing
> around with the exercises, trying to expand the and make them do things
> beyond the original programming. But I just don't understand looping.
>
> Pine says that to make a program loop you have to give a 'while'
> condition that's always true. And then provide a way to break out of the
> loop. My sample program is a teacher asking a student his name and if
> his name isn't capitalized then she'll reprimand him. If his reply isn't
> "yes" then she'll keep repeating his reply.
>
> ------------------
> puts 'What is your name, young man?'
> name = gets.chomp
>
> If name == name.capitalize
> puts 'Please take a seat, ' + name.capitalize + '.'
> else
> puts name + '? You mean, ' + name.capitalize + ' , right?'
> puts 'Don\'t you know how to spell your name?'
> reply = gets.chomp
>
> #start loop
> while reply != 'yes'
> puts reply + ' ? What!?'
> If reply == 'yes'
> break
> end
> end
> end
> -------------
>
> And the result is just the What!? part being repeated over and over
> again infinitely. So clearly I don't understand loops. Any insight?
>
> And sorry if this is an obvious mistake. I studied science in school,
> not computer science or logic but I'm trying! Thanks for any help you
> guys can give! :slight_smile:
>
> --
> Posted via http://www.ruby-forum.com/\.
>
>

Dave Aronson wrote in post #1026502:

Step through the program,
pretending to be the computer. Note the values of the variables, the
results of comparisons, any input or output, etc. Try it with
different input values. Way Back When, we used to call this "hand
simulation", dirty as that may sound. :wink:

We used to call it a "dry run", which perhaps sounds less sordid :slight_smile:

···

--
Posted via http://www.ruby-forum.com/\.