Bird
0
0

How do you write a Ruby loop that runs indefinitely without any exit condition?

easy📝 Syntax Q3 of 15
Ruby - Loops and Iteration
How do you write a Ruby loop that runs indefinitely without any exit condition?
A<code>loop do; puts 'Running'; end</code>
B<code>loop while true; puts 'Running'; end</code>
C<code>while loop; puts 'Running'; end</code>
D<code>loop until false; puts 'Running'; end</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand the loop do ... end syntax

    The loop do ... end construct runs the block repeatedly until a break is called.
  2. Step 2: Identify infinite loop syntax

    Without a break, loop do; ... end runs infinitely.
  3. Step 3: Analyze options

    loop do; puts 'Running'; end correctly uses loop do ... end with no break, creating an infinite loop.
    Options B, C, and D use incorrect or invalid syntax for infinite loops.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Does the code use loop do ... end without break? Yes. [OK]
Quick Trick: Use 'loop do ... end' without break for infinite loops [OK]
Common Mistakes:
  • Using 'loop while true' which is invalid syntax
  • Confusing 'loop until false' with infinite loop
  • Using 'while loop' which is not valid Ruby syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes