Counter-based while loop in Java - Time & Space Complexity
We want to see how the time it takes to run a counter-based while loop changes as the number it counts to gets bigger.
How does the number of steps grow when the loop runs more times?
Analyze the time complexity of the following code snippet.
int count = 0;
while (count < n) {
System.out.println(count);
count++;
}
This code prints numbers from 0 up to n-1 using a while loop that increases the counter each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop runs and prints the current count.
- How many times: It repeats exactly n times, once for each number from 0 to n-1.
As n gets bigger, the loop runs more times, so the total steps grow in a straight line with n.
| 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: Doubling n doubles the work, so the growth is steady and linear.
Time Complexity: O(n)
This means the time to finish grows directly in proportion to the size of n.
[X] Wrong: "The loop runs a fixed number of times no matter what n is."
[OK] Correct: The loop depends on n and runs once for each number up to n, so bigger n means more steps.
Understanding how loops grow with input size is a key skill. It helps you explain how your code will behave with bigger data, which is important in many real tasks.
"What if we changed the loop to count down from n to 0? How would the time complexity change?"
Practice
while loop in Java?Solution
Step 1: Understand the role of a counter
A counter is a variable that keeps track of how many times the loop has run.Step 2: Identify the purpose in a while loop
The counter helps stop the loop after a fixed number of runs, preventing infinite loops.Final Answer:
To control how many times the loop runs -> Option CQuick Check:
Counter controls loop count = A [OK]
- Thinking counter stores results
- Assuming counter creates infinite loops
- Believing counter skips iterations
Solution
Step 1: Check Java variable initialization syntax
Java usesint count = 0;to declare and initialize an integer variable.Step 2: Identify incorrect options
while (count = 0) {} uses assignment in while condition incorrectly, C uses double equals in declaration, D uses wrong assignment operator.Final Answer:
int count = 0; -> Option BQuick Check:
Correct Java int initialization = A [OK]
- Using == instead of = for assignment
- Trying to assign inside while condition
- Using := which is not Java syntax
int count = 1;
while (count <= 3) {
System.out.print(count + " ");
count++;
}Solution
Step 1: Analyze the loop start and condition
The counter starts at 1 and runs while count is less than or equal to 3.Step 2: Trace the output during each iteration
It prints 1, then increments to 2, prints 2, increments to 3, prints 3, then increments to 4 and stops.Final Answer:
1 2 3 -> Option AQuick Check:
Prints 1 to 3 with spaces = B [OK]
- Starting count at 0 instead of 1
- Printing after increment causing extra number
- Reversing the order of output
int count = 1;
while (count < 5) {
System.out.println(count);
}Solution
Step 1: Check loop body for counter update
The code does not increase count inside the loop, so count stays 1 forever.Step 2: Understand effect of missing increment
Without increment, the loop never ends, causing an infinite loop.Final Answer:
Counter variable is not incremented inside the loop -> Option AQuick Check:
Missing count++ causes infinite loop = D [OK]
- Forgetting to increment counter
- Changing loop condition incorrectly
- Assuming print syntax is wrong
Solution
Step 1: Understand the goal to print even numbers 2 to 10
We need to print 2,4,6,8,10 using a while loop and a counter.Step 2: Analyze each option
int count = 2; while (count < 10) { System.out.print(count + " " ); count += 2; } increments by 2 starting at 2, prints 2,4,6,8 misses 10. int count = 1; while (count <= 10) { if (count % 2 == 0) { System.out.print(count + " " ); } count++; } starts at 1, checks if even, prints if yes, increments by 1, correct. int count = 0; while (count < 10) { count += 2; System.out.print(count + " " ); } starts at 0, increments before print, prints 2,4,6,8 misses 10. int count = 2; while (count < 10) { System.out.print(count + " " ); count++; } increments by 1 but stops before 10, so misses 10.Step 3: Choose the best option
int count = 1; while (count <= 10) { if (count % 2 == 0) { System.out.print(count + " " ); } count++; } uses a counter-based loop with condition and checks evenness inside, which is a common pattern.Final Answer:
int count = 1; while (count <= 10) { if (count % 2 == 0) { System.out.print(count + " " ); } count++; } -> Option DQuick Check:
Check even inside loop and increment count = C [OK]
- Incrementing by 1 but missing last number
- Starting count at 0 and printing before increment
- Not checking even condition inside loop
Ready to prove it?
Pass the test and this pattern moves unknown
