If-else statements in R Programming - Time & Space Complexity
We want to see how the time it takes to run if-else statements changes as the input changes.
Are these statements quick no matter what, or do they take longer with bigger inputs?
Analyze the time complexity of the following code snippet.
value <- 7
if (value > 10) {
print("Greater than 10")
} else if (value > 5) {
print("Between 6 and 10")
} else {
print("5 or less")
}
This code checks a number and prints a message depending on its size.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single if-else checks (no loops or repeated steps)
- How many times: Exactly once per run
Checking conditions happens once, no matter the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 checks |
| 100 | 3 checks |
| 1000 | 3 checks |
Pattern observation: The number of checks stays the same even if the input number grows.
Time Complexity: O(1)
This means the time to run the if-else does not grow with input size; it stays constant.
[X] Wrong: "If-else statements take longer when the input number is bigger."
[OK] Correct: The checks happen once and do not repeat, so the size of the number does not affect how long the code runs.
Understanding that simple if-else checks run quickly no matter the input helps you explain how your code handles decisions efficiently.
"What if we added a loop that runs the if-else check for each item in a list? How would the time complexity change?"
Practice
if-else statement do in R?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 AQuick Check:
If-else chooses code path = D [OK]
- Confusing if-else with loops
- Thinking if-else stores data
- Mixing if-else with function creation
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 AQuick Check:
Correct if-else syntax uses ( ) and { } = B [OK]
- Omitting parentheses around condition
- Missing curly braces for code blocks
- Using 'then' keyword which R does not use
score <- 75
if (score >= 60) {
print("Pass")
} else {
print("Fail")
}Solution
Step 1: Evaluate the condition
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 BQuick Check:
75 >= 60 is TRUE, so output is "Pass" [OK]
- Confusing >= with >
- Thinking else block runs when condition is TRUE
- Ignoring quotes in printed output
num <- 10
if num > 5 {
print("Greater")
} else {
print("Smaller")
}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 DQuick Check:
If condition needs ( ) in R [OK]
- Omitting parentheses around condition
- Confusing curly braces placement
- Thinking else can stand alone
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 CQuick Check:
Check 2 first, then 3, else other = A [OK]
- Checking divisibility by 3 before 2
- Swapping print messages for even and odd
- Missing nested else for other cases
Ready to prove it?
Pass the test and this pattern moves unknown
