Bird
Raised Fist0
R Programmingprogramming~20 mins

While loop in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
While Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple while loop
What is the output of the following R code?
R Programming
x <- 1
while (x <= 3) {
  print(x)
  x <- x + 1
}
A
[1] 1
[1] 2
B
[1] 1
[1] 2
[1] 3
C
[1] 1
[1] 2
[1] 3
[1] 4
DError: object 'x' not found
Attempts:
2 left
💡 Hint
The loop runs while x is less than or equal to 3, printing x each time and increasing it by 1.
❓ Predict Output
intermediate
2:00remaining
While loop with break statement
What will be printed by this R code?
R Programming
i <- 1
while (TRUE) {
  if (i > 4) break
  print(i)
  i <- i + 1
}
A
[1] 1
[1] 2
[1] 3
[1] 4
B
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
CError: object 'i' not found
D
[1] 1
[1] 2
[1] 3
Attempts:
2 left
💡 Hint
The loop runs forever until the break condition is met when i is greater than 4.
❓ Predict Output
advanced
2:00remaining
While loop with nested if and continue logic
What is the output of this R code?
R Programming
x <- 0
while (x < 5) {
  x <- x + 1
  if (x == 3) next
  print(x)
}
A
[1] 1
[1] 2
[1] 4
[1] 5
B
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
C
[1] 1
[1] 2
[1] 3
[1] 4
DError: unexpected symbol in "if (x == 3) next"
Attempts:
2 left
💡 Hint
The 'next' statement skips the rest of the loop body when x equals 3.
🔧 Debug
advanced
2:00remaining
Identify the error in the while loop
What error does this R code produce?
R Programming
count <- 1
while (count < 5) {
  print(count)
  count <- count + 1
}
AInfinite loop printing 1 forever
BError: object 'count' not found
CError: unexpected symbol in 'count <- count + 1'
DPrints 1, 2, 3, 4 then stops
Attempts:
2 left
💡 Hint
Check the indentation and braces for the while loop body.
🧠 Conceptual
expert
2:00remaining
Number of iterations in a while loop
How many times will the loop run in this R code?
R Programming
n <- 10
sum <- 0
while (n > 0) {
  sum <- sum + n
  n <- n - 2
}
sum
A6
B10
C5
DError: object 'sum' not found
Attempts:
2 left
💡 Hint
The loop decreases n by 2 each time until n is no longer greater than 0.

Practice

(1/5)
1. What does a while loop do in R?
easy
A. Executes code only once
B. Repeats code a fixed number of times
C. Repeats code as long as a condition is TRUE
D. Stops the program immediately

Solution

  1. Step 1: Understand the purpose of a while loop

    A while loop runs repeatedly while its condition remains TRUE.
  2. Step 2: Compare options

    Only Repeats code as long as a condition is TRUE correctly describes this behavior; others describe different concepts.
  3. Final Answer:

    Repeats code as long as a condition is TRUE -> Option C
  4. Quick Check:

    while loop = repeat while TRUE [OK]
Hint: While loops run while condition is TRUE, not fixed times [OK]
Common Mistakes:
  • Confusing while loop with for loop
  • Thinking it runs only once
  • Believing it stops the program
2. Which of the following is the correct syntax for a while loop in R?
easy
A. while (i <= 5) { print(i); i <- i + 1 }
B. while i <= 5 { print(i); i <- i + 1 }
C. while i <= 5: print(i); i <- i + 1
D. while (i <= 5) print(i) i <- i + 1

Solution

  1. Step 1: Check R while loop syntax

    R requires parentheses around the condition and braces for the loop body.
  2. Step 2: Validate each option

    Only while (i <= 5) { print(i); i <- i + 1 } uses correct parentheses and braces syntax.
  3. Final Answer:

    while (i <= 5) { print(i); i <- i + 1 } -> Option A
  4. Quick Check:

    Correct syntax uses parentheses and braces [OK]
Hint: Use parentheses for condition and braces for loop body [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Missing braces for multiple statements
  • Using colon or wrong punctuation
3. What is the output of this R code?
i <- 1
while (i <= 3) {
  print(i)
  i <- i + 1
}
medium
A. Error: object 'i' not found
B. [1] 1 [1] 2
C. [1] 3
D. [1] 1 [1] 2 [1] 3

Solution

  1. Step 1: Trace the loop iterations

    i starts at 1, loop runs while i <= 3, printing i each time and increasing i by 1.
  2. Step 2: List printed values

    Values printed are 1, then 2, then 3 before loop stops.
  3. Final Answer:

    [1] 1 [1] 2 [1] 3 -> Option D
  4. Quick Check:

    Prints 1, 2, 3 in order [OK]
Hint: Increment loop variable inside while to avoid infinite loop [OK]
Common Mistakes:
  • Forgetting to increment i causing infinite loop
  • Expecting only last value printed
  • Confusing output format
4. Identify the error in this R while loop code:
i <- 1
while (i < 5) {
  print(i)
}
medium
A. Missing increment of i inside the loop
B. Incorrect condition syntax
C. print() function is invalid
D. Variable i is not initialized

Solution

  1. Step 1: Analyze loop body

    The loop prints i but never changes i, so i stays 1 forever.
  2. Step 2: Understand consequence

    Without increment, the condition i < 5 is always TRUE, causing infinite loop.
  3. Final Answer:

    Missing increment of i inside the loop -> Option A
  4. Quick Check:

    Update loop variable to stop loop [OK]
Hint: Always update loop variable inside while to avoid infinite loop [OK]
Common Mistakes:
  • Forgetting to increment loop variable
  • Assuming print causes increment
  • Misreading condition syntax
5. How can you use a while loop in R to calculate the factorial of 5?
Choose the correct code snippet.
hard
A. n <- 5 fact <- 0 i <- 1 while (i < n) { fact <- fact * i i <- i + 1 } fact
B. n <- 5 fact <- 1 i <- 1 while (i <= n) { fact <- fact * i i <- i + 1 } fact
C. n <- 5 fact <- 1 while (n > 0) { fact <- fact + n n <- n - 1 } fact
D. n <- 5 fact <- 1 while (fact <= n) { fact <- fact * n n <- n - 1 } fact

Solution

  1. Step 1: Understand factorial calculation

    Factorial multiplies numbers from 1 to n, so loop must multiply fact by i from 1 to n.
  2. Step 2: Check each code snippet

    n <- 5 fact <- 1 i <- 1 while (i <= n) { fact <- fact * i i <- i + 1 } fact correctly initializes fact=1, loops i from 1 to n, multiplying fact by i each time.
  3. Final Answer:

    n <- 5 fact <- 1 i <- 1 while (i <= n) { fact <- fact * i i <- i + 1 } fact -> Option B
  4. Quick Check:

    Multiply 1 to 5 sequentially for factorial [OK]
Hint: Multiply from 1 to n inside while loop for factorial [OK]
Common Mistakes:
  • Using addition instead of multiplication
  • Incorrect loop condition
  • Starting fact at 0 causing zero result
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard