0
0
R Programmingprogramming~10 mins

Repeat loop with break in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start an infinite repeat loop.

R Programming
repeat [1] { print("Hello") break }
Drag options to blanks, or click blank then click option'
A{
B(
C[
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or square brackets instead of curly braces.
Forgetting to open the block with {.
2fill in blank
medium

Complete the code to break out of the repeat loop when x is greater than 5.

R Programming
x <- 1
repeat {
  if (x [1] 5) break
  print(x)
  x <- x + 1
}
Drag options to blanks, or click blank then click option'
A<=
B>
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= or < which break too early.
Using == which breaks only when x equals 5.
3fill in blank
hard

Fix the error in the repeat loop to correctly increment x and break when x equals 3.

R Programming
x <- 0
repeat {
  x <- x [1] 1
  if (x == 3) break
  print(x)
}
Drag options to blanks, or click blank then click option'
A+
B/
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to increment x causing an infinite loop.
4fill in blank
hard

Fill both blanks to create a repeat loop that prints numbers 1 to 4 and breaks after.

R Programming
x <- 1
repeat {
  print(x)
  x <- x [1] 1
  if (x [2] 5) break
}
Drag options to blanks, or click blank then click option'
A+
B-
C>=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators for increment or condition.
Breaking too early or too late.
5fill in blank
hard

Fill all three blanks to create a repeat loop that sums numbers from 1 to 5 and breaks after.

R Programming
sum <- 0
x <- 1
repeat {
  sum <- sum [1] x
  x <- x [2] 1
  if (x [3] 6) break
}
print(sum)
Drag options to blanks, or click blank then click option'
A+
C>=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators for addition or comparison.
Breaking too early or forgetting to increment.