0
0
C Sharp (C#)programming~5 mins

Continue statement behavior in C Sharp (C#) - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Continue statement behavior
O(n)
Understanding Time Complexity

Let's see how the continue statement affects the time it takes for a loop to run.

We want to know how skipping some steps changes the total work done.

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 work here
    Console.WriteLine(i);
}
    

This code loops from 0 to n-1 but skips the loop body when the number is even.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The loop runs from 0 to n-1.
  • How many times: The loop runs n times, but the body runs only for odd numbers, about n/2 times.
How Execution Grows With Input

Even though the loop runs n times, the work inside the loop happens only half the time.

Input Size (n)Approx. Operations
1010 loop checks, 5 work executions
100100 loop checks, 50 work executions
10001000 loop checks, 500 work executions

Pattern observation: The total loop steps grow linearly with n, and the work inside grows roughly half as fast.

Final Time Complexity

Time Complexity: O(n)

This means the total time grows in a straight line as the input size increases, even with the continue skipping some steps.

Common Mistake

[X] Wrong: "Because continue skips half the work, the time complexity is O(n/2) or less."

[OK] Correct: We ignore that the loop still runs n times, so the total steps still grow linearly with n. Constants like 1/2 do not change the overall growth type.

Interview Connect

Understanding how skipping steps affects loops helps you explain code efficiency clearly and confidently in interviews.

Self-Check

What if we changed the continue condition to skip every third iteration instead of every even one? How would the time complexity change?