Bird
Raised Fist0
R Programmingprogramming~20 mins

If-else statements in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
If-Else Mastery in R
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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")
}
A[1] "Between 6 and 10"
B[1] "Greater than 10"
C[1] "5 or less"
DError in if (x > 10) { : missing value where TRUE/FALSE needed
Attempts:
2 left
💡 Hint
Check the conditions in order and see which one matches the value of x.
❓ Predict Output
intermediate
2: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")
}
AWarning: the condition has length > 1 and only the first element will be used
B[1] "No values greater than 5"
C[1] "Some values are greater than 5"
DNULL
Attempts:
2 left
💡 Hint
Remember that if expects a single TRUE or FALSE, not a vector.
🔧 Debug
advanced
2: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")
}
AError: object 'x' not found
BError in if (x > 3) { : missing value where TRUE/FALSE needed
CSyntaxError: unexpected symbol in "if x"
DNo error, prints "x is greater than 3"
Attempts:
2 left
💡 Hint
Check the syntax of the if statement in R.
🧠 Conceptual
advanced
2: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")
}
A[1] "True"
BNULL
C[1] "False"
DError in if (x) { : missing value where TRUE/FALSE needed
Attempts:
2 left
💡 Hint
Think about how R treats NA in logical conditions.
❓ Predict Output
expert
3: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")
}
A[1] "a > 3 and b < 5"
B[1] "a > 3 and b between 5 and 15"
C[1] "a > 3 and b >= 15"
D[1] "a <= 3"
Attempts:
2 left
💡 Hint
Check the values of a and b and follow the nested conditions carefully.

Practice

(1/5)
1. What does an if-else statement do in R?
easy
A. It chooses which code to run based on a condition.
B. It repeats code multiple times.
C. It stores data in a list.
D. It creates a new function.

Solution

  1. Step 1: Understand the purpose of if-else

    An if-else statement checks a condition and runs code depending on whether the condition is TRUE or FALSE.
  2. Step 2: Compare options with the definition

    Only It chooses which code to run based on a condition. describes choosing code based on a condition, which matches if-else behavior.
  3. Final Answer:

    It chooses which code to run based on a condition. -> Option A
  4. Quick Check:

    If-else chooses code path = D [OK]
Hint: If-else picks code based on TRUE or FALSE condition [OK]
Common Mistakes:
  • Confusing if-else with loops
  • Thinking if-else stores data
  • Mixing if-else with function creation
2. Which of the following is the correct syntax for an if-else statement in R?
easy
A. if (x > 0) { print("Positive") } else { print("Non-positive") }
B. if x > 0 then print("Positive") else print("Non-positive")
C. if x > 0 { print("Positive") } else { print("Non-positive") }
D. if (x > 0) print "Positive" else print "Non-positive"

Solution

  1. Step 1: Recall R if-else syntax

    In R, conditions go inside parentheses, and code blocks inside curly braces.
  2. Step 2: Check each option

    if (x > 0) { print("Positive") } else { print("Non-positive") } uses parentheses and braces correctly. Options A, C, and D miss parentheses or braces or use wrong keywords.
  3. Final Answer:

    if (x > 0) { print("Positive") } else { print("Non-positive") } -> Option A
  4. Quick Check:

    Correct if-else syntax uses ( ) and { } = B [OK]
Hint: Use parentheses for condition and braces for code blocks [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Missing curly braces for code blocks
  • Using 'then' keyword which R does not use
3. What is the output of this R code?
score <- 75
if (score >= 60) {
  print("Pass")
} else {
  print("Fail")
}
medium
A. "Fail"
B. "Pass"
C. Error
D. No output

Solution

  1. Step 1: Evaluate the condition

    The variable score is 75. The condition checks if 75 >= 60, which is TRUE.
  2. Step 2: Determine which block runs

    Since the condition is TRUE, the code inside the if block runs, printing "Pass".
  3. Final Answer:

    "Pass" -> Option B
  4. Quick Check:

    75 >= 60 is TRUE, so output is "Pass" [OK]
Hint: Check if condition is TRUE to pick output [OK]
Common Mistakes:
  • Confusing >= with >
  • Thinking else block runs when condition is TRUE
  • Ignoring quotes in printed output
4. Find the error in this R code snippet:
num <- 10
if num > 5 {
  print("Greater")
} else {
  print("Smaller")
}
medium
A. Using else without if
B. Missing curly braces around print statements
C. Variable num is not defined
D. Missing parentheses around the condition in if statement

Solution

  1. Step 1: Check if statement syntax

    In R, the condition in if must be inside parentheses. Here, it's written as if num > 5 without parentheses.
  2. Step 2: Verify other parts

    Curly braces are present, else is correctly used, and num is defined as 10.
  3. Final Answer:

    Missing parentheses around the condition in if statement -> Option D
  4. Quick Check:

    If condition needs ( ) in R [OK]
Hint: Always put condition inside parentheses in if [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Confusing curly braces placement
  • Thinking else can stand alone
5. You want to write an R program that prints "Even" if a number is divisible by 2, "Odd" if divisible by 3 but not 2, and "Other" otherwise. Which code correctly uses if-else statements?
hard
A. if (num %% 2 == 0) { print("Odd") } else { if (num %% 3 == 0) { print("Even") } else { print("Other") } }
B. if (num %% 3 == 0) { print("Odd") } else if (num %% 2 == 0) { print("Even") } else { print("Other") }
C. if (num %% 2 == 0) { print("Even") } else if (num %% 3 == 0) { print("Odd") } else { print("Other") }
D. if (num %% 2 == 0) { print("Odd") } else if (num %% 3 == 0) { print("Even") } else { print("Other") }

Solution

  1. Step 1: Understand the conditions order

    Check divisibility by 2 first to print "Even". If not divisible by 2, check divisibility by 3 to print "Odd". Otherwise print "Other".
  2. Step 2: Match code with logic

    if (num %% 2 == 0) { print("Even") } else if (num %% 3 == 0) { print("Odd") } else { print("Other") } checks num %% 2 == 0 first, then num %% 3 == 0, then else. This matches the requirement exactly.
  3. Final Answer:

    if (num %% 2 == 0) { print("Even") } else if (num %% 3 == 0) { print("Odd") } else { print("Other") } -> Option C
  4. Quick Check:

    Check 2 first, then 3, else other = A [OK]
Hint: Check conditions in correct priority order [OK]
Common Mistakes:
  • Checking divisibility by 3 before 2
  • Swapping print messages for even and odd
  • Missing nested else for other cases
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