Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i < 5' which stops the loop before printing 5.
Using 'i > 5' which never runs the loop.
✗ Incorrect
The condition i <= 5 ensures the loop runs while i is 1 through 5.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' which includes 10 and runs one extra time.
Using '>' which never runs the loop.
✗ Incorrect
The condition x < 10 stops the loop before x becomes 10.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes the loop to never run.
Using '<=' causes an infinite loop.
✗ Incorrect
The condition count > 0 ensures the loop stops when count reaches 0.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' excludes 10 from printing.
Using '-' decreases num causing an infinite loop.
✗ Incorrect
The loop runs while num is less than or equal to 10, and num increases by 2 each time.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The loop runs while i is less than or equal to 5, prints i squared, and increases i by 1 each time.