0
0
Cprogramming~5 mins

Continue statement - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Continue statement
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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).
How Execution Grows With Input

Even though some steps are skipped, the loop still checks every number up to n.

Input Size (n)Approx. Operations
1010 loop checks, ~5 sum operations
100100 loop checks, ~50 sum operations
10001000 loop checks, ~500 sum operations

Pattern observation: The total steps grow roughly in a straight line as n grows.

Final Time Complexity

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.

Common Mistake

[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.

Interview Connect

Understanding how control statements like continue affect loops helps you explain code efficiency clearly and confidently in interviews.

Self-Check

What if we replaced the continue with a nested loop inside the if-block? How would the time complexity change?