Challenge - 5 Problems
If-Else Mastery in R
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested if-else in R
What is the output of this R code?
R Programming
x <- 7 if (x > 10) { print("Greater than 10") } else if (x > 5) { print("Between 6 and 10") } else { print("5 or less") }
Attempts:
2 left
💡 Hint
Check the conditions in order and see which one matches the value of x.
✗ Incorrect
The variable x is 7, which is not greater than 10, so the first if fails. The else if checks if x is greater than 5, which is true, so it prints "Between 6 and 10".
❓ Predict Output
intermediate2:00remaining
Output of if-else with vector condition
What happens when you run this R code?
R Programming
x <- c(3, 7, 12) if (x > 5) { print("Some values are greater than 5") } else { print("No values greater than 5") }
Attempts:
2 left
💡 Hint
Remember that if expects a single TRUE or FALSE, not a vector.
✗ Incorrect
The condition x > 5 returns a vector of logicals. Using it directly in if causes a warning and only the first element is used.
🔧 Debug
advanced2:00remaining
Identify the error in this if-else code
What error does this R code produce?
R Programming
x <- 4 if x > 3 { print("x is greater than 3") } else { print("x is 3 or less") }
Attempts:
2 left
💡 Hint
Check the syntax of the if statement in R.
✗ Incorrect
In R, the condition in if must be inside parentheses. Writing 'if x > 3' without parentheses causes a syntax error.
🧠 Conceptual
advanced2:00remaining
Behavior of if-else with NA condition
What is the output of this R code?
R Programming
x <- NA if (x) { print("True") } else { print("False") }
Attempts:
2 left
💡 Hint
Think about how R treats NA in logical conditions.
✗ Incorrect
NA is an unknown logical value. Using it directly in if causes an error because R cannot decide TRUE or FALSE.
❓ Predict Output
expert3:00remaining
Output of complex nested if-else with multiple variables
What is the output of this R code?
R Programming
a <- 5 b <- 10 if (a > 3) { if (b < 5) { print("a > 3 and b < 5") } else if (b < 15) { print("a > 3 and b between 5 and 15") } else { print("a > 3 and b >= 15") } } else { print("a <= 3") }
Attempts:
2 left
💡 Hint
Check the values of a and b and follow the nested conditions carefully.
✗ Incorrect
a is 5 which is > 3, so the outer if is true. Then b is 10, which is not < 5 but is < 15, so the second condition matches and prints "a > 3 and b between 5 and 15".