Bird
Raised Fist0
Rubyprogramming~20 mins

Break, next, and redo behavior in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Loop Control Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using break?
Consider the following Ruby code snippet. What will it print when run?
Ruby
arr = [1, 2, 3, 4, 5]
result = []
arr.each do |x|
  break if x > 3
  result << x
end
puts result.inspect
A[1, 2, 3]
B[1, 2]
C[1, 2, 3, 4]
D[1, 2, 3, 4, 5]
Attempts:
2 left
💡 Hint
Remember that break exits the loop immediately when the condition is true.
❓ Predict Output
intermediate
2:00remaining
What does next do in this Ruby loop?
Look at this Ruby code. What will be printed?
Ruby
result = []
(1..5).each do |i|
  next if i.even?
  result << i
end
puts result.inspect
A[]
B[2, 4]
C[1, 2, 3, 4, 5]
D[1, 3, 5]
Attempts:
2 left
💡 Hint
next skips the current iteration and moves to the next one.
❓ Predict Output
advanced
2:00remaining
What is the output when using redo inside a loop?
Analyze this Ruby code. What will it print?
Ruby
count = 0
(1..3).each do |i|
  puts i
  count += 1
  redo if count < 3
  count = 0
end
A
1
1
1
1
1
... (infinite loop)
B
1
2
3
C
1
1
1
2
2
2
3
3
3
D
1
2
2
3
3
3
Attempts:
2 left
💡 Hint
redo repeats the current iteration without moving to the next element.
🔧 Debug
advanced
2:00remaining
Why does this Ruby code cause an infinite loop?
This Ruby code uses redo inside a loop. Why does it never end?
Ruby
i = 0
while i < 3 do
  puts i
  redo if i < 3
  i += 1
end
ABecause i is never incremented before redo repeats the iteration, so i stays 0 forever.
BBecause redo skips the i += 1 line, so i increments too fast.
CBecause the condition i < 3 is always false.
DBecause puts i causes an error stopping the loop.
Attempts:
2 left
💡 Hint
Think about what redo does to the flow of the loop and when i changes.
🧠 Conceptual
expert
3:00remaining
How do break, next, and redo differ in Ruby loops?
Which statement correctly describes the behavior of break, next, and redo in Ruby loops?
Abreak exits the entire loop immediately; next skips to the next iteration; redo repeats the current iteration without re-evaluating the loop condition.
Bbreak skips the current iteration; next exits the loop; redo moves to the next iteration.
Cbreak repeats the current iteration; next exits the loop; redo skips the current iteration.
Dbreak and next both exit the loop; redo skips the rest of the loop body.
Attempts:
2 left
💡 Hint
Think about what each keyword does to the flow of the loop.

Practice

(1/5)
1. What does the break keyword do inside a Ruby loop?
easy
A. Stops the loop completely and exits it
B. Skips the current iteration and moves to the next
C. Repeats the current iteration without moving forward
D. Pauses the loop until a condition is met

Solution

  1. Step 1: Understand the role of break

    The break keyword immediately stops the loop and exits it, no further iterations happen.
  2. Step 2: Compare with other keywords

    next skips to the next iteration, redo repeats the current one, but break stops all looping.
  3. Final Answer:

    Stops the loop completely and exits it -> Option A
  4. Quick Check:

    break = stop loop [OK]
Hint: Break means stop the loop right away [OK]
Common Mistakes:
  • Confusing break with next
  • Thinking break repeats iteration
  • Assuming break pauses instead of stops
2. Which of the following is the correct syntax to skip the current iteration in a Ruby loop?
easy
A. break
B. next
C. redo
D. skip

Solution

  1. Step 1: Identify the keyword for skipping iteration

    The keyword next is used in Ruby to skip the rest of the current loop iteration and move to the next one.
  2. Step 2: Check other options

    break stops the loop, redo repeats the current iteration, and skip is not a Ruby keyword.
  3. Final Answer:

    next -> Option B
  4. Quick Check:

    Skip iteration = next [OK]
Hint: Use next to jump to next loop step [OK]
Common Mistakes:
  • Using break instead of next
  • Trying to use skip keyword (not valid)
  • Confusing redo with next
3. What is the output of this Ruby code?
i = 0
while i < 3
  i += 1
  if i == 2
    redo
  end
  puts i
end
medium
A. 1 2 2 2 ... (infinite loop)
B. 1 3
C. 1 2 3
D. 1 3 3

Solution

  1. Step 1: Trace the loop and redo behavior

    When i == 2, redo repeats the current iteration without incrementing i again, causing the loop to print 2 repeatedly.
  2. Step 2: Understand infinite repetition

    Since i is not changed inside the redo loop, it never moves past 2, causing an infinite loop printing 2 repeatedly.
  3. Final Answer:

    1 2 2 2 ... (infinite loop) -> Option A
  4. Quick Check:

    redo repeats current step causing infinite loop [OK]
Hint: Redo repeats current step, watch for infinite loops [OK]
Common Mistakes:
  • Assuming i increments inside redo
  • Thinking redo skips iteration
  • Ignoring infinite loop possibility
4. Find the error in this Ruby code snippet:
for i in 1..3
  if i == 2
    next
  elsif i == 3
    redo
  end
  puts i
end
medium
A. puts should be before if statement
B. next cannot be used inside for loops
C. Syntax error: missing end for if
D. Using redo inside for loop causes infinite loop

Solution

  1. Step 1: Analyze the use of redo in the loop

    The redo inside the elsif i == 3 causes the loop to repeat the same iteration infinitely because i does not change.
  2. Step 2: Check other options for errors

    next works fine in for loops, the if statement is properly closed, and puts can be after if.
  3. Final Answer:

    Using redo inside for loop causes infinite loop -> Option D
  4. Quick Check:

    Redo repeats current step causing infinite loop [OK]
Hint: Redo without changing loop variable causes infinite loop [OK]
Common Mistakes:
  • Thinking next is invalid in for loops
  • Missing that redo repeats without increment
  • Assuming syntax error due to indentation
5. You want to print numbers 1 to 5 but skip 3 and repeat printing 4 twice using next and redo. Which code achieves this correctly?
i = 0
while i < 5
  i += 1
  if i == 3
    # skip 3
  elsif i == 4
    # print 4 twice
  end
  puts i
end
hard
A. if i == 3 next elsif i == 4 puts i redo next end
B. if i == 3 next elsif i == 4 redo end
C. if i == 3 next elsif i == 4 puts i redo end
D. if i == 3 redo elsif i == 4 next end

Solution

  1. Step 1: Skip number 3 using next

    Using next when i == 3 skips printing 3 and moves to next iteration.
  2. Step 2: Repeat printing 4 twice using redo

    When i == 4, print it once, then use redo to repeat the iteration and print 4 again.
  3. Step 3: Verify code correctness

    if i == 3 next elsif i == 4 puts i redo end prints 4, then calls redo to repeat printing 4, and skips 3 correctly. Other options misuse next or redo causing errors or infinite loops.
  4. Final Answer:

    if i == 3 next elsif i == 4 puts i redo end -> Option C
  5. Quick Check:

    next skips, redo repeats current step [OK]
Hint: Use next to skip, redo after puts to repeat [OK]
Common Mistakes:
  • Calling redo before puts causing infinite loop
  • Using next after redo incorrectly
  • Not skipping 3 properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard