The loop method runs the block of code inside it repeatedly forever, creating an infinite loop unless stopped with a break.
Click to reveal answer
beginner
How do you stop a loop in Ruby?
You stop a loop by using the break keyword inside the block when a certain condition is met.
Click to reveal answer
beginner
Example: What will this code do?
loop do
puts "Hello"
break
end
It will print "Hello" once and then stop because break ends the loop immediately.
Click to reveal answer
intermediate
Why use loop instead of while true for infinite loops?
loop is a Ruby method designed for infinite loops and can be more readable and expressive. It also uses a block which can be clearer to write and understand.
Click to reveal answer
beginner
What happens if you forget to use break inside a loop?
The program will keep running the loop forever, which can freeze or crash your program if not handled properly.
Click to reveal answer
What keyword stops a loop in Ruby?
Abreak
Bstop
Cexit
Dend
✗ Incorrect
The break keyword immediately exits the loop.
What does this code print?
loop do
puts "Hi"
break
end
APrints "Hi" forever
BPrints "Hi" once
CPrints nothing
DCauses an error
✗ Incorrect
The loop prints "Hi" once and then breaks out of the loop.
Which of these is a correct way to create an infinite loop in Ruby?
Aif true then ... end
Bfor i in 1..10 do ... end
Cloop do ... end
Dwhile false do ... end
✗ Incorrect
loop do ... end runs forever unless broken.
What will happen if you run loop do puts 'Hello' end without a break?
APrints 'Hello' once
BThrows an error
CDoes not print anything
DPrints 'Hello' forever
✗ Incorrect
Without break, the loop runs forever printing 'Hello'.
Why might you prefer loop do over while true?
A<code>loop</code> is more readable and idiomatic in Ruby
B<code>while true</code> is faster
C<code>loop</code> cannot be broken
DThere is no difference
✗ Incorrect
loop is a Ruby method designed for infinite loops and is often clearer.
Explain how the loop method works in Ruby and how you can stop it.
Think about what happens inside the block and how to exit.
You got /3 concepts.
Describe a situation where using loop do ... end is helpful and how to avoid problems with infinite loops.
Consider real-life tasks that repeat until a condition is met.
You got /4 concepts.
Practice
(1/5)
1. What does the Ruby code loop do; puts "Hello"; break; end do?
easy
A. Prints "Hello" once and stops the loop
B. Prints "Hello" infinitely without stopping
C. Causes a syntax error
D. Prints nothing
Solution
Step 1: Understand the loop structure
The loop do ... end creates an infinite loop that repeats the code inside.
Step 2: Analyze the break statement
The break immediately stops the loop after printing "Hello" once.
Final Answer:
Prints "Hello" once and stops the loop -> Option A
Quick Check:
loop with break stops after one iteration [OK]
Hint: Remember: break stops the infinite loop immediately [OK]
Common Mistakes:
Thinking loop runs forever even with break
Confusing loop with while
Ignoring the break statement
2. Which of the following is the correct syntax to create an infinite loop using the loop method in Ruby?
easy
A. loop: do puts "Hi" end
B. loop { puts "Hi" do end }
C. loop puts "Hi" do end
D. loop { puts "Hi" }
Solution
Step 1: Review Ruby loop syntax
The loop method can take a block with curly braces or do...end.
Step 2: Identify correct syntax
loop { puts "Hi" } uses loop { puts "Hi" }, which is valid Ruby syntax for an infinite loop.
Final Answer:
loop { puts "Hi" } -> Option D
Quick Check:
loop with block braces is valid syntax [OK]
Hint: Use loop with { } or do...end for blocks [OK]
Common Mistakes:
Placing do after loop without block
Using colon incorrectly
Missing block braces or do...end
3. What will be the output of the following Ruby code?
count = 0
loop do
puts count
count += 1
break if count == 3
end
medium
A. 0 1 2 3
B. 0 1 2
C. 1 2 3
D. Infinite loop printing numbers
Solution
Step 1: Trace the loop iterations
Initially, count = 0. The loop prints count, then increments it by 1.
Step 2: Check break condition
Loop breaks when count == 3, so it stops after printing 0, 1, and 2.
Final Answer:
0 1 2 -> Option B
Quick Check:
Loop stops before printing 3 due to break condition [OK]
Hint: Break stops loop before count reaches 3 [OK]
Common Mistakes:
Including 3 in output
Forgetting to increment count
Assuming infinite loop
4. Identify the error in the following Ruby code using loop do:
loop do
puts "Start"
break
puts "End"
end
medium
A. The code after break will never execute
B. The break statement is unreachable
C. Syntax error due to missing end
D. Infinite loop without break
Solution
Step 1: Understand break behavior
The break stops the loop immediately when reached.
Step 2: Analyze code after break
The puts "End" after break will never run because break exits the loop first.
Final Answer:
The code after break will never execute -> Option A
Quick Check:
Code after break is skipped [OK]
Hint: Code after break inside loop is ignored [OK]
Common Mistakes:
Thinking code after break runs
Confusing break with next
Assuming syntax error
5. How can you use the Ruby loop do method to repeatedly ask a user for input until they type "exit"?
hard
A. Use loop without break and rely on user to close program
B. Use while loop with condition input != "exit"
C. Use loop with gets and break if input equals "exit"
D. Use for loop iterating over input strings
Solution
Step 1: Use loop to repeat input request
The loop do method runs code repeatedly until broken.
Step 2: Use break with condition on user input
Inside the loop, get user input with gets.chomp and break if input is "exit".
Final Answer:
Use loop with gets and break if input equals "exit" -> Option C
Quick Check:
loop + break on condition = controlled infinite loop [OK]
Hint: Break loop when input matches exit string [OK]
Common Mistakes:
Not using break to stop loop
Using wrong loop type
Forgetting to remove newline from input
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.