0
0
Javaprogramming~5 mins

Continue statement in Java - Time & Space Complexity

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

Let's see how using the continue statement affects the time complexity of a loop.

We want to know how the number of steps changes as the input 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;
    }
    System.out.println(i);
}
    

This code loops from 0 to n-1, skips even numbers using continue, and prints only 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 print happens only for about half of them.
How Execution Grows With Input

As n grows, the loop runs more times, but continue skips some steps.

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

Pattern observation: The loop runs n times, but the print runs about n/2 times. The continue skips printing for half the cases.

Final Time Complexity

Time Complexity: O(n)

This means the total steps grow linearly with the input size, even with continue skipping some steps.

Common Mistake

[X] Wrong: "Using continue makes the loop run faster and reduces time complexity."

[OK] Correct: Continue only skips some steps inside the loop but the loop still runs n times, so the overall time grows linearly with n.

Interview Connect

Understanding how continue affects loops helps you explain code behavior clearly and shows you can analyze performance carefully.

Self-Check

"What if we replaced continue with a nested if statement that only prints when i is odd? How would the time complexity change?"