Continue statement - Time & Space Complexity
Let's see how the continue statement affects the time complexity of a loop in C.
We want to know how the number of steps changes as the input size grows.
Analyze the time complexity of the following code snippet.
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
continue;
}
// Some constant time operation
sum += i;
}
This code loops from 0 to n-1, skips even numbers using continue, and sums odd numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop runs from 0 to n-1.
- How many times: The loop runs n times, but the sum operation runs only for odd i values (about n/2 times).
Even though some steps are skipped, the loop still checks every number up to n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 loop checks, ~5 sum operations |
| 100 | 100 loop checks, ~50 sum operations |
| 1000 | 1000 loop checks, ~500 sum operations |
Pattern observation: The total steps grow roughly in a straight line as n grows.
Time Complexity: O(n)
This means the time to run the loop grows directly with the size of n, even with the continue skipping some steps.
[X] Wrong: "Since continue skips some steps, the loop runs faster and time complexity is less than O(n)."
[OK] Correct: The loop still checks every number once, so the total steps still grow with n. Skipping inside the loop does not reduce the number of loop iterations.
Understanding how control statements like continue affect loops helps you explain code efficiency clearly and confidently in interviews.
What if we replaced the continue with a nested loop inside the if-block? How would the time complexity change?
Practice
continue statement do inside a loop in C?Solution
Step 1: Understand the role of
Thecontinuecontinuestatement does not stop the loop but skips the remaining code in the current iteration.Step 2: Effect on loop execution
Aftercontinue, the loop immediately proceeds to the next iteration, ignoring any code below it in the current cycle.Final Answer:
Skips the rest of the current loop iteration and moves to the next iteration -> Option AQuick Check:
continueskips current iteration [OK]
- Confusing continue with break
- Thinking continue exits the function
- Assuming continue restarts the program
continue inside a for loop in C?Solution
Step 1: Recall correct statement syntax
In C,continueis a statement and must end with a semicolon.Step 2: Identify valid syntax
Onlycontinue;is valid; parentheses, braces, or brackets are incorrect.Final Answer:
continue; -> Option AQuick Check:
Statement ends with semicolon [OK]
- Adding parentheses after continue
- Using braces or brackets with continue
- Omitting the semicolon
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
printf("%d ", i);
}Solution
Step 1: Analyze loop iterations
The loop runs i from 1 to 5. When i equals 3, the continue statement skips printing.Step 2: Determine printed values
Values 1, 2, 4, and 5 are printed with spaces. The number 3 is skipped.Final Answer:
1 2 4 5 -> Option BQuick Check:
continue skips printing 3 [OK]
- Including the skipped number in output
- Confusing continue with break
- Ignoring the loop condition
continue:
int i = 0;
while (i < 5) {
if (i == 2)
continue;
printf("%d ", i);
i++;
}Solution
Step 1: Understand loop flow with continue
When i equals 2, continue skips the rest of the loop body, so i++ is not executed.Step 2: Consequence of skipping i++
Since i is not incremented at i == 2, the loop condition remains true forever, causing an infinite loop.Final Answer:
The loop will run infinitely because 'i' is not incremented when i == 2 -> Option CQuick Check:
continue skips i++ causing infinite loop [OK]
- Thinking continue causes syntax error
- Assuming printf is unreachable
- Ignoring loop variable increment
continue?
for (int i = 1; i <= 10; i++) {
printf("%d ", i);
}Solution
Step 1: Identify condition to skip even numbers
Even numbers satisfyi % 2 == 0. Using continue here skips printing even numbers.Step 2: Place continue before printf
Addingif (i % 2 == 0) continue;before printf skips printing even numbers, so only odd numbers print.Final Answer:
Addif (i % 2 == 0) continue;before printf -> Option DQuick Check:
continue skips even numbers, prints odd [OK]
- Skipping odd numbers instead of even
- Using continue after printf
- Incorrect condition for even numbers
Ready to prove it?
Pass the test and this pattern moves unknown
