Recall & Review
beginner
What is a
repeat loop in R?A
repeat loop in R runs the code inside it indefinitely until a break statement stops it.Click to reveal answer
beginner
How do you stop a
repeat loop in R?You stop a
repeat loop by using the break statement inside the loop when a condition is met.Click to reveal answer
beginner
What happens if you forget to use
break in a repeat loop?The loop will run forever, causing your program to freeze or crash because it never stops.
Click to reveal answer
beginner
Example: What does this code do?<br>
i <- 1<br>repeat {<br> print(i)<br> if (i == 3) break<br> i <- i + 1<br>}This code prints numbers 1, 2, and 3. It stops the loop when
i equals 3 using break.Click to reveal answer
intermediate
Why use a
repeat loop instead of a for or while loop?Use
repeat when you want to keep looping until a condition inside the loop tells it to stop, without specifying the number of iterations upfront.Click to reveal answer
What keyword stops a
repeat loop in R?✗ Incorrect
The
break keyword immediately stops the loop.What happens if a
repeat loop has no break?✗ Incorrect
Without
break, the repeat loop never stops and runs forever.Which of these is a correct way to stop a
repeat loop when i equals 5?✗ Incorrect
The correct syntax to stop the loop is
if (i == 5) break.What will this code print?<br>
i <- 1<br>repeat {<br> print(i)<br> if (i >= 2) break<br> i <- i + 1<br>}✗ Incorrect
It prints 1 first, then 2, then stops because
i >= 2 becomes true.Why might you choose a
repeat loop over a while loop?✗ Incorrect
A
repeat loop runs indefinitely until a break inside the loop stops it, so you check the stop condition inside the loop body.Explain how a
repeat loop works in R and how you stop it.Think about what happens if you never use break.
You got /3 concepts.
Write a simple example of a
repeat loop that prints numbers 1 to 4 and then stops.Use a variable to count and break when it reaches 4.
You got /4 concepts.