While loop in C - Time & Space Complexity
We want to see how the time a while loop takes changes as the input size grows.
How does the number of steps increase when the loop runs more times?
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 that repeats until i reaches n, doing a small task each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop running the code inside.
- How many times: It runs once for each number from 0 up to n - 1, so n times.
Each time n gets bigger, the loop runs more times, adding more steps.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The number of steps grows directly with n; if n doubles, steps double.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the size of n.
[X] Wrong: "The while loop always takes the same time no matter what n is."
[OK] Correct: The loop runs n times, so bigger n means more steps and more time.
Understanding how loops grow with input helps you explain your code clearly and shows you know how programs scale.
"What if we changed the loop to increase i by 2 each time? How would the time complexity change?"
Practice
while loop do in C programming?Solution
Step 1: Understand the purpose of a while loop
A while loop runs repeatedly as long as its condition remains true.Step 2: Compare options with the definition
Only Repeats a block of code while a condition is true correctly describes this behavior.Final Answer:
Repeats a block of code while a condition is true -> Option AQuick Check:
While loop = repeat while condition true [OK]
- Thinking while loop runs at least once even if condition is false
- Confusing while loop with for loop fixed iterations
- Believing while loop stops program immediately
Solution
Step 1: Recall C while loop syntax
The condition must be inside parentheses and the code block inside braces.Step 2: Check each option
Only while (condition) { /* code */ } uses correct parentheses and braces syntax.Final Answer:
while (condition) { /* code */ } -> Option AQuick Check:
Correct syntax uses parentheses and braces [OK]
- Omitting parentheses around condition
- Using square brackets instead of parentheses
- Adding colon after while keyword
int i = 1;
while (i <= 3) {
printf("%d ", i);
i++;
}Solution
Step 1: Trace the loop variable values
i starts at 1, prints 1, then increments to 2, prints 2, then 3, and stops after printing 3 because i becomes 4 and condition fails.Step 2: Confirm printed output
The printed numbers are "1 2 3 " with spaces after each number.Final Answer:
1 2 3 -> Option DQuick Check:
Prints 1 to 3 with spaces [OK]
- Including 4 in output by mistake
- Starting from 0 instead of 1
- Printing numbers in reverse order
int i = 0;
while (i < 5)
printf("%d ", i);
Solution
Step 1: Analyze loop behavior
Variable i starts at 0 and condition is i < 5, but i is never incremented inside the loop.Step 2: Understand consequence
Since i never changes, the condition stays true forever, causing an infinite loop.Final Answer:
Infinite loop because i is never updated -> Option CQuick Check:
Loop variable must change to avoid infinite loop [OK]
- Assuming loop ends without variable update
- Thinking missing braces cause syntax error here
- Ignoring infinite loop possibility
int num = 1;
while (num <= 10) {
printf("%d ", num);
num++;
}Solution
Step 1: Set starting point and increment for even numbers
Start num at 2 (first even number) and increase by 2 each time to get only even numbers.Step 2: Modify loop accordingly
Change initialization to num = 2 and inside loop do num += 2 instead of num++.Final Answer:
Initialize num to 2 and increment by 2 inside the loop -> Option BQuick Check:
Start at 2, add 2 each time for evens [OK]
- Not incrementing num causing infinite loop
- Printing odd numbers by starting at 1
- Using for loop when question asks for while loop
Ready to prove it?
Pass the test and this pattern moves unknown
