While loop in C++ - Time & Space Complexity
We want to understand how the time a while loop takes changes when the input size changes.
Specifically, how many times does the loop run as input grows?
Analyze the time complexity of the following code snippet.
int i = 0;
while (i < n) {
// some constant time work
i++;
}
This code runs a loop from 0 up to n-1, doing a small task each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop runs repeatedly.
- How many times: It runs once for each number from 0 up to n-1, so n times.
As n gets bigger, the loop runs more times, growing in a straight line with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: If you double n, the work roughly doubles too.
Time Complexity: O(n)
This means the time grows directly in proportion to the input size.
[X] Wrong: "The while loop runs forever or a fixed number of times regardless of n."
[OK] Correct: The loop clearly depends on n and runs exactly n times, so the time changes with input size.
Understanding how loops grow with input is a key skill that helps you explain code efficiency clearly and confidently.
"What if we changed the increment from i++ to i += 2? How would the time complexity change?"
Practice
What does a while loop do in C++?
Solution
Step 1: Understand the purpose of a while loop
A while loop runs the code inside it repeatedly as long as the condition is true.Step 2: Compare options with definition
Only Repeats code while a condition is true correctly states this behavior.Final Answer:
Repeats code while a condition is true -> Option DQuick Check:
while loop = repeat while true [OK]
- Thinking while loops run a fixed number of times
- Confusing while with if statement
- Assuming while loops always run once
Which of the following is the correct syntax for a while loop in C++?
???
{
// code
}Solution
Step 1: Recall correct while loop syntax
The condition must be inside parentheses after the keyword while.Step 2: Check each option
while (condition) useswhile (condition), which is correct syntax in C++.Final Answer:
while (condition) -> Option CQuick Check:
while loop syntax = while (condition) [OK]
- Omitting parentheses around condition
- Using curly braces instead of parentheses
- Adding colon after while
What is the output of this C++ code?
int i = 1;
while (i <= 3) {
std::cout << i << " ";
i++;
}Solution
Step 1: Analyze loop condition and increments
Variable i starts at 1 and increases by 1 each loop until i > 3.Step 2: Trace output values
i = 1 prints "1 ", then 2 prints "2 ", then 3 prints "3 ". Loop stops after i becomes 4.Final Answer:
1 2 3 -> Option BQuick Check:
Prints 1 to 3 with spaces [OK]
- Printing extra number after loop ends
- Starting count from 0 instead of 1
- Printing numbers in reverse
Find the error in this code snippet:
int count = 5;
while (count > 0)
std::cout << count << " ";Solution
Step 1: Check loop body and variable changes
The loop prints count but does not change it inside the loop.Step 2: Understand loop behavior
Since count stays 5, condition is always true, causing an infinite loop.Final Answer:
Infinite loop because count is never changed -> Option AQuick Check:
Loop variable must change to avoid infinite loop [OK]
- Forgetting to change loop variable
- Assuming loop ends automatically
- Misreading syntax as error
Write a while loop in C++ that prints all even numbers from 2 to 10 inclusive.
Which code snippet correctly does this?
???
Solution
Step 1: Understand the requirement
We need to print even numbers from 2 to 10 inclusive, so numbers: 2,4,6,8,10.Step 2: Check each option
int n = 2; while (n <= 10) { std::cout << n << " "; n += 2; } starts at 2, increments by 2, and stops at 10 inclusive, printing correct numbers.Final Answer:
2 4 6 8 10 -> Option AQuick Check:
Start at 2, add 2 each time, stop at 10 [OK]
- Stopping before 10 (using < instead of <=)
- Starting at 1 and missing even numbers
- Including 0 which is not in range
Ready to prove it?
Pass the test and this pattern moves unknown
