For loop in C - Time & Space Complexity
We want to understand how the time a for loop takes changes as we increase the number of times it runs.
How does the work grow when the loop runs more times?
Analyze the time complexity of the following code snippet.
for (int i = 0; i < n; i++) {
printf("%d\n", i);
}
This code prints numbers from 0 up to n-1 using a for loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for loop runs the print statement.
- How many times: Exactly n times, once for each number from 0 to n-1.
As n grows, the number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print operations |
| 100 | 100 print operations |
| 1000 | 1000 print operations |
Pattern observation: The work grows directly with n. Double n, double the work.
Time Complexity: O(n)
This means the time grows in a straight line with the number of loop runs.
[X] Wrong: "The loop runs faster because it just prints numbers quickly."
[OK] Correct: The speed of printing does not change how many times the loop runs. The loop still runs n times, so time grows with n.
Understanding how loops affect time helps you explain how your code scales and shows you can think about efficiency clearly.
"What if we added a second nested for loop inside this one? How would the time complexity change?"
Practice
for loop in C primarily do?Solution
Step 1: Understand the purpose of a for loop
A for loop is designed to repeat a block of code multiple times based on a condition.Step 2: Identify the correct description
Among the options, only repeating code a fixed number of times matches the for loop's purpose.Final Answer:
Repeats a block of code a fixed number of times -> Option AQuick Check:
For loop = repeat code [OK]
- Confusing for loop with variable declaration
- Thinking for loop stops program
- Mixing for loop with function declaration
Solution
Step 1: Review standard for loop syntax in C
The correct syntax is: for (initialization; condition; update) { body }Step 2: Check each option
for (int i = 0; i < 5; i++) { } matches the correct syntax with initialization, condition, and update in proper order.Final Answer:
for (int i = 0; i < 5; i++) { } -> Option DQuick Check:
Syntax = for(init; cond; update) [OK]
- Using 'for i = 0 to 5' which is not C syntax
- Swapping condition and initialization
- Missing semicolons inside for parentheses
for(int i = 1; i <= 3; i++) {
printf("%d ", i);
}Solution
Step 1: Analyze the loop range
The loop starts at i=1 and runs while i is less than or equal to 3, incrementing i by 1 each time.Step 2: Determine printed values
It prints i each time: 1, then 2, then 3, then stops.Final Answer:
1 2 3 -> Option CQuick Check:
Loop prints 1 to 3 [OK]
- Starting loop at 0 instead of 1
- Including an extra number beyond 3
- Printing in reverse order
for(int i = 0; i < 5; i)
printf("%d ", i);Solution
Step 1: Examine the for loop update part
The update section is 'i' without increment operator, so i never changes.Step 2: Understand the effect of missing increment
Without 'i++' or similar, the loop runs infinitely or behaves incorrectly.Final Answer:
Missing increment operator in for loop update -> Option BQuick Check:
Update must change loop variable [OK]
- Forgetting to increment loop variable
- Confusing semicolon placement
- Assuming variable auto-increments
Solution
Step 1: Understand the goal
We want to print even numbers from 2 to 10 inclusive.Step 2: Check each option
for(int i = 2; i <= 10; i += 2) { printf("%d ", i); } starts at 2 and increments by 2, printing 2,4,6,8,10 exactly.Final Answer:
for(int i = 2; i <= 10; i += 2) { printf("%d ", i); } -> Option AQuick Check:
Increment by 2 prints evens directly [OK]
- Using wrong increment step
- Off-by-one errors in loop condition
- Printing odd numbers by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
