While loop in R Programming - Time & Space Complexity
We want to understand how the time a while loop takes changes as the input grows.
Specifically, how many times does the loop run when the input gets bigger?
Analyze the time complexity of the following code snippet.
i <- 1
while (i <= n) {
print(i)
i <- i + 1
}
This code prints numbers from 1 up to n using a while loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop runs and prints a number each time.
- How many times: It runs once for each number from 1 to n, so n times.
As n grows, the number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 prints and increments |
| 100 | About 100 prints and increments |
| 1000 | About 1000 prints and increments |
Pattern observation: The work grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time it takes grows in a straight line with the size of n.
[X] Wrong: "The while loop runs a fixed number of times no matter what n is."
[OK] Correct: The loop depends on n, so if n gets bigger, the loop runs more times.
Understanding how loops grow with input size helps you explain your code clearly and shows you know how to write efficient programs.
"What if we changed the loop to increase i by 2 each time? How would the time complexity change?"
Practice
while loop do in R?Solution
Step 1: Understand the purpose of a while loop
A while loop runs repeatedly while its condition remains TRUE.Step 2: Compare options
Only Repeats code as long as a condition is TRUE correctly describes this behavior; others describe different concepts.Final Answer:
Repeats code as long as a condition is TRUE -> Option CQuick Check:
while loop = repeat while TRUE [OK]
- Confusing while loop with for loop
- Thinking it runs only once
- Believing it stops the program
Solution
Step 1: Check R while loop syntax
R requires parentheses around the condition and braces for the loop body.Step 2: Validate each option
Only while (i <= 5) { print(i); i <- i + 1 } uses correct parentheses and braces syntax.Final Answer:
while (i <= 5) { print(i); i <- i + 1 } -> Option AQuick Check:
Correct syntax uses parentheses and braces [OK]
- Omitting parentheses around condition
- Missing braces for multiple statements
- Using colon or wrong punctuation
i <- 1
while (i <= 3) {
print(i)
i <- i + 1
}Solution
Step 1: Trace the loop iterations
i starts at 1, loop runs while i <= 3, printing i each time and increasing i by 1.Step 2: List printed values
Values printed are 1, then 2, then 3 before loop stops.Final Answer:
[1] 1 [1] 2 [1] 3 -> Option DQuick Check:
Prints 1, 2, 3 in order [OK]
- Forgetting to increment i causing infinite loop
- Expecting only last value printed
- Confusing output format
i <- 1
while (i < 5) {
print(i)
}Solution
Step 1: Analyze loop body
The loop prints i but never changes i, so i stays 1 forever.Step 2: Understand consequence
Without increment, the condition i < 5 is always TRUE, causing infinite loop.Final Answer:
Missing increment of i inside the loop -> Option AQuick Check:
Update loop variable to stop loop [OK]
- Forgetting to increment loop variable
- Assuming print causes increment
- Misreading condition syntax
Choose the correct code snippet.
Solution
Step 1: Understand factorial calculation
Factorial multiplies numbers from 1 to n, so loop must multiply fact by i from 1 to n.Step 2: Check each code snippet
n <- 5 fact <- 1 i <- 1 while (i <= n) { fact <- fact * i i <- i + 1 } fact correctly initializes fact=1, loops i from 1 to n, multiplying fact by i each time.Final Answer:
n <- 5 fact <- 1 i <- 1 while (i <= n) { fact <- fact * i i <- i + 1 } fact -> Option BQuick Check:
Multiply 1 to 5 sequentially for factorial [OK]
- Using addition instead of multiplication
- Incorrect loop condition
- Starting fact at 0 causing zero result
Ready to prove it?
Pass the test and this pattern moves unknown
