0
0
R Programmingprogramming~10 mins

While loop 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 print numbers from 1 to 5 using a while loop.

R Programming
i <- 1
while (i [1] 5) {
  print(i)
  i <- i + 1
}
Drag options to blanks, or click blank then click option'
A>=
B<=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i < 5' which stops the loop before printing 5.
Using 'i > 5' which never runs the loop.
2fill in blank
medium

Complete the code to stop the loop when x reaches 10.

R Programming
x <- 1
while (x [1] 10) {
  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 '<=' which includes 10 and runs one extra time.
Using '>' which never runs the loop.
3fill in blank
hard

Fix the error in the loop condition to avoid an infinite loop.

R Programming
count <- 5
while (count [1] 0) {
  print(count)
  count <- count - 1
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes the loop to never run.
Using '<=' causes an infinite loop.
4fill in blank
hard

Fill both blanks to create a loop that prints even numbers from 2 to 10.

R Programming
num <- 2
while (num [1] 10) {
  print(num)
  num <- num [2] 2
}
Drag options to blanks, or click blank then click option'
A<=
B<
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' excludes 10 from printing.
Using '-' decreases num causing an infinite loop.
5fill in blank
hard

Fill all three blanks to create a loop that prints squares of numbers from 1 to 5.

R Programming
i <- 1
while (i [1] 5) {
  print(i [2] i)
  i <- i [3] 1
}
Drag options to blanks, or click blank then click option'
A<=
B*
C+
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' excludes 5 from printing.
Using '+' instead of '*' prints double i, not square.
Using '-' decreases i causing infinite loop.