Why control flow directs execution in R Programming - Performance Analysis
Control flow decides which parts of code run and how often. This affects how long a program takes to finish.
We want to see how the program's steps grow as input changes.
Analyze the time complexity of the following code snippet.
for (i in 1:n) {
if (i %% 2 == 0) {
print(i)
} else {
print(i * 2)
}
}
This code runs a loop from 1 to n and uses an if-else to decide what to print each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop runs through numbers 1 to n.
- How many times: Exactly n times, once for each number.
Each time n grows, the loop runs more times, doing one check and one print per number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and 10 prints |
| 100 | About 100 checks and 100 prints |
| 1000 | About 1000 checks and 1000 prints |
Pattern observation: The work grows directly with n, doubling n doubles the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the input size.
[X] Wrong: "The if-else makes the code run twice as long because it has two paths."
[OK] Correct: Only one path runs each time, so the total steps still match the loop count.
Understanding how control flow affects steps helps you explain code speed clearly. This skill shows you can think about program behavior beyond just writing code.
"What if we added a nested loop inside the if block? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of control flow
Control flow guides the program to run certain parts of code based on conditions or loops.Step 2: Compare options with control flow purpose
Only Decides which parts of the code run and when matches this role; others describe unrelated actions.Final Answer:
Decides which parts of the code run and when -> Option CQuick Check:
Control flow = directs execution [OK]
- Confusing control flow with variable creation
- Thinking control flow changes data types
- Assuming control flow prints output
Solution
Step 1: Recall R if statement syntax
In R, the if statement requires parentheses around the condition and braces for the code block.Step 2: Check each option
if (x > 5) { print("Yes") } uses correct parentheses and braces. Options B, C, and D use incorrect syntax styles.Final Answer:
if (x > 5) { print("Yes") } -> Option BQuick Check:
R if syntax = if(condition) { code } [OK]
- Omitting parentheses around condition
- Using 'then' keyword (not in R)
- Missing braces for code block
for (i in 1:3) {
if (i == 2) {
print("Skip")
next
}
print(i)
}Solution
Step 1: Trace the for loop iterations
The loop runs i = 1, 2, 3. For i=1, it prints 1. For i=2, it prints "Skip" and uses next to skip printing i. For i=3, it prints 3.Step 2: Understand the effect of next
next skips the rest of the loop body for i=2, so print(i) is not called then.Final Answer:
[1] 1 [1] "Skip" [1] 3 -> Option AQuick Check:
next skips print(i) at i=2 [OK]
- Expecting number 2 to print
- Ignoring next statement effect
- Confusing print order
i <- 1
while (i < 4) {
print(i)
i = i + 1
}Solution
Step 1: Check while loop syntax
The while loop uses parentheses for condition and braces for the body, which is correct.Step 2: Check variable increment
In R, '=' can be used for assignment inside code blocks, so 'i = i + 1' works fine.Final Answer:
No error, code runs correctly -> Option DQuick Check:
Both '=' and '<-' assign values in R [OK]
- Thinking '=' is invalid for assignment
- Expecting syntax error without braces
- Confusing loop condition syntax
Solution
Step 1: Understand the goal
We want to print numbers 1, 2, 4, 5 but skip printing 3.Step 2: Analyze each option
for (i in 1:5) { if (i == 3) next; print(i) } uses 'next' to skip printing when i is 3, printing all others. for (i in 1:5) { if (i == 3) break; print(i) } stops loop at 3 (break), so 4 and 5 won't print. for (i in 1:5) { print(i); if (i == 3) next } prints i first then next skips the rest of the body (which is empty), so prints all including 3. for (i in 1:5) { if (i == 3) print(i); else next } prints only 3 and skips others, which is wrong.Step 3: Choose the best control flow usage
Only for (i in 1:5) { if (i == 3) next; print(i) } correctly uses next to skip exactly 3 and continue the loop.Final Answer:
for (i in 1:5) { if (i == 3) next; print(i) } -> Option AQuick Check:
next skips iteration cleanly [OK]
- Using break stops loop early
- Printing only the skipped number
- Not using next to skip iteration
- Placing print before the next check
Ready to prove it?
Pass the test and this pattern moves unknown
