Continue statement in Java - Time & Space 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.
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 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.
As n grows, the loop runs more times, but continue skips some steps.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 loop checks, 5 prints |
| 100 | 100 loop checks, 50 prints |
| 1000 | 1000 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.
Time Complexity: O(n)
This means the total steps grow linearly with the input size, even with continue skipping some steps.
[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.
Understanding how continue affects loops helps you explain code behavior clearly and shows you can analyze performance carefully.
"What if we replaced continue with a nested if statement that only prints when i is odd? How would the time complexity change?"
Practice
continue statement do inside a loop in Java?Solution
Step 1: Understand the role of continue
Thecontinuestatement skips the remaining code in the current loop iteration.Step 2: Effect on loop execution
After skipping, the loop moves to the next iteration without stopping the entire loop.Final Answer:
Skips the rest of the current loop iteration and moves to the next iteration -> Option AQuick Check:
continue skips current step [OK]
- Confusing continue with break
- Thinking continue exits the method
- Assuming continue restarts the program
continue inside a for loop in Java?Solution
Step 1: Recall Java continue syntax
Thecontinuestatement is used alone followed by a semicolon inside loops.Step 2: Check each option
continue; uses correct syntax:continue;. Others are invalid syntax in Java.Final Answer:
continue; -> Option AQuick Check:
Correct continue syntax ends with semicolon [OK]
- Adding parentheses after continue
- Using labels without proper syntax
- Adding brackets or other symbols
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
System.out.print(i + " ");
}Solution
Step 1: Analyze loop iterations
The loop runs i from 1 to 5. When i equals 3, the continue skips printing.Step 2: Determine printed values
Values printed are 1, 2, 4, 5 with spaces after each.Final Answer:
1 2 4 5 -> Option BQuick Check:
Skip i=3, print others [OK]
- Including 3 in output
- Stopping loop at 3
- Printing without spaces
for (int i = 0; i < 5; i++) {
if (i == 2)
continue
System.out.println(i);
}Solution
Step 1: Check syntax of continue
The continue statement must end with a semicolon in Java.Step 2: Identify missing semicolon
Code misses semicolon after continue, causing syntax error.Final Answer:
Missing semicolon after continue statement -> Option CQuick Check:
continue requires semicolon [OK]
- Omitting semicolon after continue
- Thinking continue can't be in for loops
- Misplacing print statement
continue to print only odd numbers from 1 to 10 in Java?Solution
Step 1: Identify condition to skip even numbers
To print only odd numbers, skip even numbers using continue.Step 2: Apply continue inside loop
When number is even, continue skips printing and moves to next iteration.Final Answer:
Use continue when number is even to skip printing -> Option DQuick Check:
Skip even numbers with continue [OK]
- Skipping odd numbers instead of even
- Using break instead of continue
- Placing continue outside loop
Ready to prove it?
Pass the test and this pattern moves unknown
