Loop execution flow in C++ - Time & Space Complexity
When we look at loops in code, we want to know how many times the steps inside run as the input grows.
We ask: How does the work increase when the loop runs more times?
Analyze the time complexity of the following code snippet.
for (int i = 0; i < n; i++) {
// some simple operation
sum += i;
}
This code runs a loop from 0 up to n-1 and adds each number to sum.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The addition inside the loop (sum += i)
- How many times: Exactly n times, once for each loop cycle
As n grows, the number of additions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly in step with n. Double n, double the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the size of n.
[X] Wrong: "The loop runs faster because it just adds numbers, so time is constant."
[OK] Correct: Even simple steps add up when repeated many times. More loops mean more total work.
Understanding how loops grow helps you explain your code clearly and shows you know how programs scale.
"What if we added a nested loop inside this loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of loops
Loops are used to repeat a set of instructions multiple times without writing the code again and again.Step 2: Identify the correct purpose
Among the options, only repeating code matches the loop's purpose.Final Answer:
To repeat a block of code multiple times -> Option AQuick Check:
Loop purpose = repeat code [OK]
- Confusing loops with variables
- Thinking loops create new data types
- Believing loops stop program immediately
for loop in C++?Solution
Step 1: Recall the correct for loop syntax
The for loop syntax requires parentheses around initialization, condition, and increment, followed by a block in braces.Step 2: Check each option
for (int i = 0; i < 5; i++) { /* code */ } matches the correct syntax exactly. Others miss parentheses, braces, or use wrong symbols.Final Answer:
for (int i = 0; i < 5; i++) { /* code */ } -> Option CQuick Check:
Correct for loop syntax = for (int i = 0; i < 5; i++) { /* code */ } [OK]
- Omitting parentheses around conditions
- Missing braces for loop body
- Using wrong symbols like [] instead of {}
int count = 0;
for (int i = 1; i <= 3; i++) {
count += i;
}
std::cout << count;Solution
Step 1: Trace the loop iterations
The loop runs with i = 1, 2, 3. Each time, count adds i: 0+1=1, 1+2=3, 3+3=6.Step 2: Determine final count value
After the loop, count is 6, which is printed.Final Answer:
6 -> Option BQuick Check:
Sum 1+2+3 = 6 [OK]
- Confusing loop end condition
- Forgetting to add inside loop
- Assuming count resets inside loop
int i = 0;
while (i < 3) {
std::cout << i << " ";
}Solution
Step 1: Analyze the while loop behavior
The loop condition is i < 3, but i never changes inside the loop, causing an infinite loop.Step 2: Identify the missing increment
To progress, i must increase inside the loop, e.g., i++.Final Answer:
Missing increment of i inside the loop -> Option DQuick Check:
Loop needs variable update to avoid infinite loop [OK]
- Forgetting to increment loop variable
- Thinking syntax error causes infinite loop
- Declaring variable inside loop unnecessarily
int sum = 0;
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) continue;
sum += i;
}
std::cout << sum;What is the output and why?
Solution
Step 1: Understand the loop and condition
The loop runs i from 1 to 5. If i is even, continue skips adding it to sum.Step 2: Calculate sum of odd numbers
Odd numbers are 1, 3, 5. Their sum is 1+3+5 = 9.Final Answer:
9, because only odd numbers 1, 3, 5 are added -> Option AQuick Check:
Sum odd 1+3+5 = 9 [OK]
- Adding even numbers by mistake
- Ignoring continue statement effect
- Summing all numbers instead of filtered ones
Ready to prove it?
Pass the test and this pattern moves unknown
