- Understand how the
break
keyword is used in looping. - Understand how the
gets
method is used to store a user's input. - Utilize both
break
andgets
in your own program.
What happens when we have an infinite loop that keeps on going forever? What happens when, given a specific output, we want to break out of that loop? This is where the break
keyword comes in.
Let's take a look at the following example:
counter = 1
while counter <= 5
puts "The counter is at: #{counter}"
break if counter == 3
counter = counter + 1
end
puts "We're done looping!"
We've added a break
keyword in the while
loop. When the condition if counter == 3
is met, it will break out of the loop completely, and it will not continue to iterate all the way up to 5
. What's going to be the output?
"The counter is at: 1"
"The counter is at: 2"
"The counter is at: 3"
"We're done looping!"
Let's say we are writing a program in order to annoy someone (maybe a younger sibling or beloved coworker?) Our program should puts
the phrase "What's up, Doc?" again and again and again until our annoyed user finally inputs "STOP" into the terminal. In this case, we are looping indefinitely and breaking if a user inputs "STOP".
After we discuss when NOT to use the break
keyword, we will show you how to get and use a user's input.
The use of the break
keyword is generally avoided. In upcoming lessons, we'll learn how to write more elegant, self-explanatory loops that don't require break
-ing. It is important to understand how break
works, however, and it is useful in command line applications.
Let's think about our example from earlier. We want a program that continuously outputs "Whats up, Doc?" until the user can't take it anymore and finally inputs "STOP". In other words, we need to create a loop that will continue to ask the user "What's up, Doc?", but will break out of the loop when the user types in "STOP".
This will require us to get some user input and to break the loop accordingly.
Check out the following method:
def annoying
loop do
puts "What's up, Doc?"
answer = gets.chomp
break if answer == "STOP"
end
puts "Okay, okay, jeez. I'll stop. Sorry."
end
annoying
If you type the above code into IRB (hint: do it), you will probably be annoyed. Your interaction should go something like this:
What's up, Doc?
nothing
What's up, Doc?
I said nothing
What's up, Doc?
nothing!
What's up, Doc?
ugh
What's up, Doc?
quit it
What's up, Doc?
stop it
What's up, Doc?
stop
What's up, Doc?
stop
What's up, Doc?
STOP
Okay, okay, jeez. I'll stop. Sorry.
=> nil
Let's break this down. We have a method, called annoying
, that contains a loop. The loop says:
puts
the phrase "What's up, Doc?"- Collect the user's input via the
gets.chomp
method and store them in a variable calledanswer
. - Check to see if the
answer
variable points to a string value of"STOP"
. - If it doesn't, go back to the top of the loop and repeat. If it does,
break
out of the loop and proceed to the next line of code:puts "Okay, okay, jeez. I'll stop. Sorry."
Fork and clone this lab. Read the below explanation and run the test suite to get started.
-
Okay, now that we've mastered feather levitation, we're ready for the levitation quiz! Fill out the content of the method
levitation_quiz
so that it contains a loop that asks the user "What is the spell that enacts levitation?" It should then store the answer in a variable calledanswer
using thegets.chomp
method. The loop shouldbreak
if the user's answer is "Wingardium Leviosa". When the loop breaks, our method shouldputs
"You passed the quiz!" Otherwise, the loop should continue. -
Important: The Learn gem cannot interact with
gets.chomp
in the command line. To get your tests passing when you runrspec
, make sure you are not calling the method you define anywhere inlevitation_quiz.rb
. You can call the method and run the program withruby levitation_quiz.rb
to play with your program and see it working, but to run the tests withrspec
, comment out or delete the line where you call the method.