Continue statement in C++ - Time & Space Complexity
We want to see how using the continue statement affects how long a program takes to run.
Specifically, does skipping some steps change the overall work done as input 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 work here
int x = i * 2;
}
This code loops through numbers from 0 to n-1, skipping even numbers using continue.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop runs from 0 to n-1.
- How many times: It runs n times, but the work inside after
continueruns only for odd i values.
As n grows, the loop runs n times, but the work after continue happens only about 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 runs grow linearly with n, and the work inside grows roughly half as fast.
Time Complexity: O(n)
This means the program's running time grows in a straight line as the input size increases, even with the continue skipping some steps.
[X] Wrong: "Using continue makes the loop run faster and changes the time complexity to less than linear."
[OK] Correct: The loop still runs n times; skipping some steps only reduces work by a constant factor, not the overall growth pattern.
Understanding how control statements like continue affect time helps you explain your code clearly and reason about efficiency in real projects.
"What if the continue condition depended on a nested loop? How would that change the time complexity?"
Practice
continue statement do inside a loop in C++?Solution
Step 1: Understand the role of
Thecontinuein loopscontinuestatement tells the program to skip the remaining code in the current loop cycle.Step 2: Identify what happens after
After skipping, the loop moves to the next iteration immediately without executing the rest of the loop body.continueFinal Answer:
It skips the rest of the current loop iteration and moves to the next iteration. -> Option BQuick Check:
Continue skips current iteration [OK]
- Thinking continue stops the loop completely
- Confusing continue with break
- Assuming continue exits the function
continue inside a for loop in C++?Solution
Step 1: Recall the syntax of
Thecontinuein C++continuestatement is a keyword and used alone followed by a semicolon.Step 2: Check each option's syntax
Onlycontinue;is valid. The others use parentheses or braces which are incorrect.Final Answer:
continue; -> Option AQuick Check:
Continue 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;
std::cout << i << " ";
}Solution
Step 1: Analyze the loop and condition
The loop runs from 1 to 5. Wheni == 3, thecontinueskips printing.Step 2: Determine printed values
Numbers 1 and 2 print normally. At 3, continue skips printing. Then 4 and 5 print.Final Answer:
1 2 4 5 -> Option CQuick Check:
Skip 3, print others [OK]
- Including 3 in output
- Stopping loop at 3
- Printing numbers after 3 incorrectly
for (int i = 0; i < 5; i++) {
if (i == 2)
continue
std::cout << i << " ";
}Solution
Step 1: Check syntax of continue statement
Thecontinuestatement must end with a semicolon.Step 2: Identify missing semicolon
In the code,continueis not followed by a semicolon, causing a syntax error.Final Answer:
Missing semicolon after continue statement. -> Option DQuick Check:
Continue needs semicolon [OK]
- Forgetting semicolon after continue
- Assuming continue needs parentheses
- Misreading loop condition as error
continue. What is the final sum?int sum = 0;
for (int i = 1; i <= 10; i++) {
if (i % 3 == 0) continue;
sum += i;
}
std::cout << sum;Solution
Step 1: Identify numbers skipped by continue
Numbers divisible by 3 (3, 6, 9) are skipped and not added to sum.Step 2: Calculate sum of numbers 1 to 10 excluding 3, 6, 9
Sum 1+2+4+5+7+8+10 = 37 + 15 = 52Final Answer:
52 -> Option AQuick Check:
Sum excluding multiples of 3 = 52 [OK]
- Including multiples of 3 in sum
- Adding all numbers 1 to 10
- Misunderstanding continue effect
Ready to prove it?
Pass the test and this pattern moves unknown
