An if-else statement lets the program choose between two paths based on a condition. If the condition is true, it runs one block of code; if false, it runs another.
Click to reveal answer
beginner
How do you write a simple if statement in R?
Use the syntax: if (condition) { code } where 'condition' is a test that returns TRUE or FALSE, and 'code' runs only if the condition is TRUE.
Click to reveal answer
beginner
What happens if the condition in an if statement is FALSE and there is no else?
The code inside the if block is skipped, and the program continues with the next lines after the if statement.
Click to reveal answer
beginner
How do you write an if-else statement in R?
Use: if (condition) { code if TRUE } else { code if FALSE }. This runs one block if the condition is TRUE, otherwise the other block.
Click to reveal answer
intermediate
Can you have multiple conditions checked in R using if-else?
Yes, by using else if between if and else, you can check multiple conditions in order.
Click to reveal answer
What will this R code print? if (5 > 3) { print("Yes") } else { print("No") }
AYes
BNo
C5
DError
✗ Incorrect
Since 5 is greater than 3, the condition is TRUE, so it prints "Yes".
Which keyword is used to check an additional condition after an if in R?
Aelseif
Belse if
Celif
Delseif()
✗ Incorrect
In R, the correct syntax is 'else if' with a space, not 'elseif' or 'elif'.
What happens if the condition in an if statement is FALSE and there is no else?
AThe code inside if is skipped
BThe code inside if runs anyway
CThe program crashes
DThe else block runs
✗ Incorrect
If the condition is FALSE and no else is present, the if block is skipped.
Which of these is the correct syntax for an if-else statement in R?
Aif x > 0 then print("Positive") else print("Non-positive")
Option C uses the correct 'else if' syntax with parentheses.
Explain how an if-else statement controls the flow of a program in R.
Think about how the program chooses which code to run based on a test.
You got /4 concepts.
Write an example of an if-else statement in R that prints "Even" if a number is even, and "Odd" if it is odd.
Use the modulo operator %% to check if a number is divisible by 2.
You got /4 concepts.
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
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.
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.
Final Answer:
It chooses which code to run based on a condition. -> Option A
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
Step 1: Recall R if-else syntax
In R, conditions go inside parentheses, and code blocks inside curly braces.
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.
Final Answer:
if (x > 0) { print("Positive") } else { print("Non-positive") } -> Option A
Quick Check:
Correct if-else syntax uses ( ) and { } = B [OK]
Hint: Use parentheses for condition and braces for code blocks [OK]
The variable score is 75. The condition checks if 75 >= 60, which is TRUE.
Step 2: Determine which block runs
Since the condition is TRUE, the code inside the if block runs, printing "Pass".
Final Answer:
"Pass" -> Option B
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
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.
Step 2: Verify other parts
Curly braces are present, else is correctly used, and num is defined as 10.
Final Answer:
Missing parentheses around the condition in if statement -> Option D
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
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".
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.
Final Answer:
if (num %% 2 == 0) {
print("Even")
} else if (num %% 3 == 0) {
print("Odd")
} else {
print("Other")
} -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.