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 P.'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:

‘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

Yes, that makes sense! Thanks for your help!

“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

Dave A. 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:

On Tue, Oct 11, 2011 at 11:27, Dennis N. [email protected]
wrote:

But I need some help to understand looping.

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