Need help with while loop

Hi I am going the self taught route on learning ruby. I have a question
and hope that this is the right place to ask questions.
My question is:

How do I get a while statement to terminate? I know I can use an if
conditional with break to get out of a while statement. But what I want
to do is to be able to add float numbers without being asked each time
if I want to quit. I would like to just add numbers and when I am done
I will type the string “no”.
I can do this if I use floating numbers/integers but not able to find a
way to do this with a string.
ie.
while score >= 0

case answer

when “y”

  grades.push score
  puts "Enter another grade from 1.0 to 100.0 or 0 to quit! "
  score = gets.to_f

the programme will quit once a value less than zero has been entered. I
would like to be able to do this with a string. If this requires a
nested while loop that would be fine.

cheers,
Wolf

You can use multiple conditions, or you could use “break” with a
separate conditional.

Thank you Joel.

I have tried with if and break. For example I used something like

puts "To quit use ‘n’ "
answer = gets.chomp
if answer == ‘n’
break
end
But this means that I have to put up with being asked “To quit use ‘n’”
with every iteration.
Would yo be so kind as to give examples of multiple conditions ie.
separate conditionals.

while is not beautiful.

Use loop:

loop {

_ = $stdin.gets.chomp
case _
when ‘q’,‘exit’
exit
else
puts 'You did input: '+_
end

}

If you need it in a program, replace exit with break.

What is your normal input? When writing a conditional, it helps to have
examples to test against.

I’ll run with the assumption that it’s Float vs anything else for now.

You can test whether a number is a Float using Float(number), which
raises an error when given invalid input.
You can rescue the error, allowing you do something different based on
the outcome.

answer = gets

begin
answer = Float(answer)
puts “Answer: #{ answer }”
rescue ArgumentError
puts ‘Invalid input, terminating loop.’
break
end

“Would yo be so kind as to give examples of multiple
conditions ie. separate conditionals.”

Use case/when menu in Ruby.

You have to use and understand it only once, then you
can write all future programs with this in mind.

Thank you both for replying. I had already used a case statement but
the problem with that is again for every iteration I am asked whether I
want to
add a value where I then have to reply with a y or n to continue. I
want to be able to add values and then when I want to quit just input an
‘n’ to quit.

Example of using case:

#!/usr/bin/env ruby

enter scores to get avg of scores

grades = []
total = 0.0
avg = 0.0

answer = “y”
while answer == “y”
puts "do you want to add another grade to the avg? "
answer = gets.chomp

case answer
when “y”
puts "Add a grade from 0.0 to 100.0 "
score = gets.to_i
grades.push score

when “n”
puts “No more scores will be added calculating the avg now!”
end

  puts grades.inspect

puts total

end

    grades.each do |item|
      total += item
    end

puts total
no_grades = grades.length
puts no_grades
avg = total / no_grades

puts “the average grade is #{avg}”

But I want to use a while statement that allows me to enter ‘n’ when I
want to quit, but at the same time I want to be able to add values
without being asked each time.

kind regards.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs