Challenge - 5 Problems
Control Flow Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this R code with if-else?
Consider the following R code snippet. What will it print?
R Programming
x <- 5 if (x > 3) { print("Greater") } else { print("Smaller") }
Attempts:
2 left
💡 Hint
Check the condition x > 3 and what it evaluates to.
✗ Incorrect
Since x is 5, which is greater than 3, the if block runs and prints "Greater".
❓ Predict Output
intermediate2:00remaining
What does this for loop print in R?
Look at this R code. What will be the output?
R Programming
for (i in 1:3) { if (i == 2) next print(i) }
Attempts:
2 left
💡 Hint
The 'next' statement skips the current iteration.
✗ Incorrect
When i is 2, 'next' skips printing 2. So only 1 and 3 are printed.
❓ Predict Output
advanced2:00remaining
What is the output of this nested if-else in R?
Analyze this R code and select the correct output.
R Programming
x <- 0 if (x > 0) { print("Positive") } else if (x == 0) { print("Zero") } else { print("Negative") }
Attempts:
2 left
💡 Hint
Check the value of x and which condition matches.
✗ Incorrect
x equals 0, so the else if branch runs and prints "Zero".
❓ Predict Output
advanced2:00remaining
What happens when this R code runs with break inside a while loop?
What will this R code print?
R Programming
i <- 1 while (i <= 5) { if (i == 3) break print(i) i <- i + 1 }
Attempts:
2 left
💡 Hint
The break statement stops the loop when i equals 3.
✗ Incorrect
The loop prints 1 and 2, then breaks when i is 3, so 3 is not printed.
❓ Predict Output
expert3:00remaining
What is the value of x after this R code with nested control flow?
Consider this R code. What is the final value of x?
R Programming
x <- 0 for (i in 1:4) { if (i %% 2 == 0) { x <- x + i } else { x <- x - i } }
Attempts:
2 left
💡 Hint
Add or subtract i depending on whether it is even or odd.
✗ Incorrect
For i=1 (odd), x=0-1=-1; i=2 (even), x=-1+2=1; i=3 (odd), x=1-3=-2; i=4 (even), x=-2+4=2.