Why loops are needed in C++ - Performance Analysis
Loops help us repeat actions many times without writing the same code again and again.
We want to know how the time to run the code changes when we repeat tasks more times.
Analyze the time complexity of the following code snippet.
int sum = 0;
for (int i = 0; i < n; i++) {
sum += i;
}
return sum;
This code adds numbers from 0 up to n-1 and returns the total sum.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding a number to sum inside the loop.
- How many times: The loop runs n times, once for each number from 0 to n-1.
Each time n gets bigger, the loop runs more times, so the work grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: When n doubles, the work doubles too.
Time Complexity: O(n)
This means the time to finish grows directly with the number of times we repeat the loop.
[X] Wrong: "The loop runs only once, so it takes constant time."
[OK] Correct: The loop runs n times, so the time depends on n, not just once.
Understanding how loops affect time helps you explain how your code handles bigger tasks clearly and confidently.
"What if we added a second nested loop inside the first? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of loops
Loops allow repeating the same instructions many times without rewriting the code.Step 2: Compare other options
Options A, C, and D describe other programming concepts, not loops.Final Answer:
To repeat a set of instructions multiple times without writing them again -> Option AQuick Check:
Loops repeat actions [OK]
- Confusing loops with variables
- Thinking loops speed up code by skipping
- Mixing loops with data storage
for loop in C++?Solution
Step 1: Recall correct for loop syntax
The correct syntax uses: for (initialization; condition; update) { code }Step 2: Check each option
for (int i = 0; i < 5; i++) { /* code */ } matches the correct syntax. Options A, B, and C have syntax errors.Final Answer:
for (int i = 0; i < 5; i++) { /* code */ } -> Option CQuick Check:
Standard for loop syntax [OK]
- Using 'for i = 0 to 5' like other languages
- Writing 'loop' instead of 'for'
- Mixing order of initialization and condition
for (int i = 1; i <= 3; i++) {
std::cout << i << " ";
}Solution
Step 1: Analyze loop range and output
The loop starts at i=1 and runs while i <= 3, printing i each time.Step 2: List printed values
Values printed are 1, 2, and 3, each followed by a space.Final Answer:
1 2 3 -> Option BQuick Check:
Loop prints 1 to 3 [OK]
- Starting loop at 0 instead of 1
- Including extra number beyond 3
- Printing in reverse order
int i = 0;
while (i < 3) {
std::cout << i << " ";
}Solution
Step 1: Check loop structure
The while loop condition is correct and variable i is declared.Step 2: Look for loop update
There is no statement changing i inside the loop, so i stays 0 forever, causing infinite loop.Final Answer:
No update to i inside the loop causing infinite loop -> Option DQuick Check:
Loops need variable update to end [OK]
- Forgetting to increment loop variable
- Assuming loop ends automatically
- Misreading syntax errors
Solution
Step 1: Consider loop control and efficiency
Starting at 2 and incrementing by 2 prints only even numbers directly.Step 2: Compare other options
Use a while loop starting at 1, incrementing by 1, and print if even checks each number if even, less efficient. Use a do-while loop starting at 10, decrementing by 2 until 2 counts backward, which is okay but less natural here. Use a for loop starting at 0, incrementing by 1, and print if even starts at 0 and prints even numbers but includes 0 and extra checks.Final Answer:
Use a for loop with i starting at 2, incrementing by 2 until 10 -> Option AQuick Check:
Directly loop over even numbers [OK]
- Using loops that check every number unnecessarily
- Starting loop at wrong number
- Confusing increment direction
Ready to prove it?
Pass the test and this pattern moves unknown
