For loop in R Programming - Time & Space Complexity
We want to understand how the time it takes to run a for loop changes as we increase the number of times it runs.
Basically, how does the work grow when the loop runs more times?
Analyze the time complexity of the following code snippet.
for (i in 1:n) {
print(i)
}
This code prints numbers from 1 up to n, running the loop n times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The print statement inside the for loop.
- How many times: Exactly n times, once for each number from 1 to n.
As n grows, the number of print operations grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: The work grows directly in proportion to n. Double n, double the work.
Time Complexity: O(n)
This means the time to run the loop grows in a straight line with the number of times it runs.
[X] Wrong: "The loop runs faster because it just prints numbers quickly."
[OK] Correct: Even if printing is fast, the loop still does one print per number, so the total work grows with n.
Understanding how loops grow with input size helps you explain your code's efficiency clearly and confidently in real situations.
"What if we added a nested for loop inside this loop? How would the time complexity change?"
Practice
for loop do in R programming?Solution
Step 1: Understand the purpose of a for loop
A for loop is used to repeat actions multiple times, once for each item in a sequence.Step 2: Match the description to the options
Repeats a block of code for each item in a sequence correctly describes this behavior, while others describe unrelated actions.Final Answer:
Repeats a block of code for each item in a sequence -> Option DQuick Check:
For loop = repeat for each item [OK]
- Confusing for loop with function definition
- Thinking for loop stops the program
- Believing for loop creates variables only
Solution
Step 1: Recall R for loop syntax
In R, the for loop uses the format: for (variable in sequence) { code }Step 2: Compare options to correct syntax
for (i in 1:5) { print(i) } matches the correct syntax. Options A, B, and D use syntax from other languages or incorrect forms.Final Answer:
for (i in 1:5) { print(i) } -> Option BQuick Check:
R for loop = for (var in sequence) [OK]
- Using 'for i = 1 to 5' like other languages
- Using C-style for loop syntax
- Writing 'loop' instead of 'for'
for (i in 1:3) {
print(i * 2)
}Solution
Step 1: Understand the loop iterations
The loop runs with i = 1, 2, 3.Step 2: Calculate print output for each i
For i=1: print(2), i=2: print(4), i=3: print(6).Final Answer:
[1] 2 [1] 4 [1] 6 -> Option AQuick Check:
Multiply i by 2 each loop [OK]
- Printing i instead of i*2
- Confusing output format
- Expecting a single combined print
for i in 1:4 {
print(i)
}Solution
Step 1: Check for correct for loop syntax in R
R requires parentheses around the loop variable and sequence: for (i in 1:4) { ... }Step 2: Identify the error in given code
The code misses parentheses around 'i in 1:4', causing a syntax error.Final Answer:
Missing parentheses around the loop variable and sequence -> Option CQuick Check:
For loop needs parentheses ( ) [OK]
- Omitting parentheses around 'i in 1:4'
- Confusing braces and parentheses
- Thinking print syntax is wrong
Solution
Step 1: Initialize an empty vector to store results
squares <- c() for (i in 1:5) { squares[i] <- i^2 } squares initializes 'squares' as an empty vector to store squares.Step 2: Use a for loop to assign squares to each position
Inside the loop, squares[i] <- i^2 correctly stores the square of i at position i.Step 3: Check other options for errors
for (i in 1:5) { squares <- i^2 } squares overwrites 'squares' each time, losing previous values. squares <- 1:5 for (i in squares) { squares[i] = i * 2 } squares multiplies by 2, not square. squares <- c() for i in 1:5 { squares[i] <- i^2 } squares misses parentheses in for loop syntax.Final Answer:
squares <- c() for (i in 1:5) { squares[i] <- i^2 } squares -> Option AQuick Check:
Initialize vector, assign squares in loop [OK]
- Not initializing vector before loop
- Overwriting vector inside loop
- Wrong loop syntax without parentheses
Ready to prove it?
Pass the test and this pattern moves unknown
