0
0
R Programmingprogramming~20 mins

Repeat loop with break in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Repeat Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
}
A
[1] 1
[1] 2
[1] 3
[1] 4
B
[1] 1
[1] 2
C
[1] 1
[1] 2
[1] 3
DError: object 'count' not found
Attempts:
2 left
💡 Hint
The loop stops when count becomes greater than 3.
Predict Output
intermediate
2: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)
}
A
[1] 7
[1] 4
[1] 1
B
[1] 7
[1] 4
[1] 1
[1] -2
CError: object 'x' not found
D
[1] 7
[1] 4
Attempts:
2 left
💡 Hint
The loop subtracts 3 from x each time and prints x only if it is greater than 0.
🔧 Debug
advanced
2: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
}
ANo error, prints numbers 1 to 5
BSyntaxError: unexpected end of input
CError: object 'i' not found
DWarning: infinite loop detected
Attempts:
2 left
💡 Hint
Check if the break statement is correctly placed without braces.
Predict Output
advanced
2: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
}
AError: object 'inner' not found
B
"1 1"
"1 2"
"2 1"
"2 2"
C
"1 1"
"2 1"
"3 1"
D
"1 1"
"1 2"
"2 1"
"2 2"
"3 1"
"3 2"
Attempts:
2 left
💡 Hint
The inner loop breaks when inner equals 2, the outer loop breaks when outer equals 3.
🧠 Conceptual
expert
2:00remaining
Behavior of break in repeat loops
Which statement about the break statement in R's repeat loops is TRUE?
Abreak can only be used with for loops, not repeat loops
Bbreak exits only the innermost repeat loop it is called in
Cbreak pauses the loop and resumes after a condition is met
Dbreak exits all nested repeat loops immediately
Attempts:
2 left
💡 Hint
Think about nested loops and how break affects them.