Challenge - 5 Problems
Repeat Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a repeat loop with break
What is the output of this R code using a repeat loop with a break condition?
R Programming
count <- 1 repeat { if (count > 3) { break } print(count) count <- count + 1 }
Attempts:
2 left
💡 Hint
The loop stops when count becomes greater than 3.
✗ Incorrect
The repeat loop prints count starting at 1 and increments it. When count becomes 4, the break stops the loop, so only 1, 2, and 3 are printed.
❓ Predict Output
intermediate2:00remaining
Repeat loop with break and condition
What will this R code print?
R Programming
x <- 10 repeat { x <- x - 3 if (x <= 0) { break } print(x) }
Attempts:
2 left
💡 Hint
The loop subtracts 3 from x each time and prints x only if it is greater than 0.
✗ Incorrect
x starts at 10, then becomes 7, 4, 1. When x becomes -2, the break stops the loop before printing.
🔧 Debug
advanced2:00remaining
Identify the error in this repeat loop
What error does this R code produce?
R Programming
i <- 1 repeat { print(i) if (i == 5) break i <- i + 1 }
Attempts:
2 left
💡 Hint
Check if the break statement is correctly placed without braces.
✗ Incorrect
In R, if the if statement has only one line, braces are optional. The code runs fine and prints 1 to 5.
❓ Predict Output
advanced2:00remaining
Repeat loop with nested break
What is the output of this R code with nested repeat loops and break?
R Programming
outer <- 1 repeat { inner <- 1 repeat { print(paste(outer, inner)) if (inner == 2) { break } inner <- inner + 1 } if (outer == 3) { break } outer <- outer + 1 }
Attempts:
2 left
💡 Hint
The inner loop breaks when inner equals 2, the outer loop breaks when outer equals 3.
✗ Incorrect
The code prints pairs of outer and inner values. Inner runs from 1 to 2 for each outer from 1 to 3.
🧠 Conceptual
expert2:00remaining
Behavior of break in repeat loops
Which statement about the break statement in R's repeat loops is TRUE?
Attempts:
2 left
💡 Hint
Think about nested loops and how break affects them.
✗ Incorrect
In R, break exits only the loop where it is called, not outer loops.