Continue statement behavior in C Sharp (C#) - Time & Space 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.
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 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.
Even though the loop runs n times, the work inside the loop happens only half the time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 loop checks, 5 work executions |
| 100 | 100 loop checks, 50 work executions |
| 1000 | 1000 loop checks, 500 work executions |
Pattern observation: The total loop steps grow linearly with n, and the work inside grows roughly half as fast.
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.
[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.
Understanding how skipping steps affects loops helps you explain code efficiency clearly and confidently in interviews.
What if we changed the continue condition to skip every third iteration instead of every even one? How would the time complexity change?
Practice
continue statement do inside a loop in C#?Solution
Step 1: Understand the role of
Thecontinuein loopscontinuestatement tells the loop to skip the remaining code in the current iteration.Step 2: Identify what happens after
After skipping, the loop moves to the next iteration without stopping the whole loop.continueFinal Answer:
Skips the rest of the current loop iteration and moves to the next one -> Option BQuick Check:
continueskips current iteration [OK]
- Confusing continue with break
- Thinking continue exits the method
- Assuming continue repeats the iteration
continue inside a for loop in C#?Solution
Step 1: Recall the syntax of
In C#,continuecontinueis a statement and must be followed by a semicolon.Step 2: Check each option
Onlycontinue;is valid syntax. The others use parentheses, braces, or brackets incorrectly.Final Answer:
continue; -> Option AQuick Check:
Correct syntax 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;
Console.Write(i + " ");
}Solution
Step 1: Analyze the loop and condition
The loop runs from 1 to 5. Wheniequals 3,continueskips printing.Step 2: Determine printed values
Numbers 1, 2, 4, and 5 are printed with spaces. Number 3 is skipped.Final Answer:
1 2 4 5 -> Option DQuick Check:
continue skips printing 3 [OK]
- Including the skipped number in output
- Confusing continue with break
- Ignoring spaces in output
continue:
int i = 0;
while (i < 3) {
if (i == 1)
continue;
Console.Write(i);
i++;
}Solution
Step 1: Understand loop behavior with continue
Wheni == 1,continueskipsi++, soistays 1 forever.Step 2: Identify the problem caused
The loop never incrementsipast 1, causing an infinite loop.Final Answer:
Infinite loop because i++ is skipped when i == 1 -> Option CQuick Check:
continue skips i++ causing infinite loop [OK]
- Thinking continue causes syntax error
- Ignoring loop variable increment
- Assuming Console.WriteLine fixes logic
for loop and continue. Which code snippet correctly does this?Solution
Step 1: Understand the goal
Print even numbers 2,4,8,10 but skip 6 using continue inside a for loop.Step 2: Check each option
for (int i = 2; i <= 10; i += 2) { if (i == 6) continue; Console.Write(i + " "); } loops by 2, skips 6 with continue, prints others. for (int i = 1; i <= 10; i++) { if (i % 2 == 0 && i != 6) Console.Write(i + " "); } prints all evens except 6 but no continue used. for (int i = 2; i <= 10; i++) { if (i % 2 == 0) Console.Write(i + " "); if (i == 6) continue; } prints 6 because print happens before continue. for (int i = 2; i <= 10; i += 2) { if (i == 6) break; Console.Write(i + " "); } breaks at 6, stopping loop early, so 8 and 10 not printed.Final Answer:
for (int i = 2; i <= 10; i += 2) { if (i == 6) continue; Console.Write(i + " "); } -> Option AQuick Check:
Use continue to skip 6 in even-step loop [OK]
- Using break instead of continue
- Not incrementing by 2 for evens
- Skipping continue and using if only
Ready to prove it?
Pass the test and this pattern moves unknown
