Bird
0
0

How can you use the Ruby loop do method to repeatedly ask a user for input until they type "exit"?

hard📝 Application Q15 of 15
Ruby - Loops and Iteration
How can you use the Ruby loop do method to repeatedly ask a user for input until they type "exit"?
AUse loop without break and rely on user to close program
BUse while loop with condition input != "exit"
CUse loop with gets and break if input equals "exit"
DUse for loop iterating over input strings
Step-by-Step Solution
Solution:
  1. Step 1: Use loop to repeat input request

    The loop do method runs code repeatedly until broken.
  2. Step 2: Use break with condition on user input

    Inside the loop, get user input with gets.chomp and break if input is "exit".
  3. Final Answer:

    Use loop with gets and break if input equals "exit" -> Option C
  4. Quick Check:

    loop + break on condition = controlled infinite loop [OK]
Quick Trick: Break loop when input matches exit string [OK]
Common Mistakes:
MISTAKES
  • Not using break to stop loop
  • Using wrong loop type
  • Forgetting to remove newline from input

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes