Continue statement in Go - Time & Space Complexity
Let's see how the continue statement affects the time a program takes to run.
We want to know if skipping some steps changes how long the code runs as input grows.
Analyze the time complexity of the following code snippet.
for i := 0; i < n; i++ {
if i%2 == 0 {
continue
}
// some constant time work here
}
This code loops from 0 to n-1, but skips the work when i is even using continue.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single loop running n times.
- How many times: The loop runs n times, but work inside runs about half the times.
Even though some steps are skipped, the loop still checks all n items.
| 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 steps grow linearly with n, skipping work only halves some operations.
Time Complexity: O(n)
This means the time grows directly with the input size, even if some steps are skipped.
[X] Wrong: "Using continue makes the loop run faster and changes time complexity to O(n/2)."
[OK] Correct: Even if some work is skipped, the loop still runs n times, so time still grows linearly with n.
Understanding how continue affects loops helps you explain code efficiency clearly and shows you know how skipping steps impacts performance.
What if we replaced continue with break? How would the time complexity change?
Practice
continue statement do inside a loop in Go?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 on to the next iteration without stopping the whole loop.continueFinal Answer:
Skips the rest of the current loop iteration and moves to the next iteration -> Option AQuick Check:
continueskips current step [OK]
- Confusing continue with break
- Thinking continue exits the function
- Assuming continue repeats the iteration
continue inside a for loop in Go?Solution
Step 1: Recall Go syntax for statements
In Go,continueis a keyword used alone without parentheses or semicolons.Step 2: Identify the correct form
The correct usage is simplycontinueby itself inside the loop.Final Answer:
continue -> Option AQuick Check:
Go statements like continue have no parentheses or semicolons [OK]
- Adding semicolon after continue
- Using continue as a function with parentheses
- Using braces {} after continue
for i := 1; i <= 5; i++ {
if i == 3 {
continue
}
fmt.Print(i, " ")
}Solution
Step 1: Analyze the loop and condition
The loop runs from 1 to 5. Wheniequals 3,continueskips printing 3.Step 2: Determine printed values
Values 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 3 in output
- Stopping loop instead of skipping
- Printing nothing due to misunderstanding continue
continue:
for i := 0; i < 5; i++ {
if i == 2 {
continue()
}
fmt.Println(i)
}Solution
Step 1: Check the usage of continue
In Go,continueis a statement without parentheses. Usingcontinue()causes a syntax error.Step 2: Verify other parts
The loop condition andfmt.Printlnusage are correct. The error is only in the continue syntax.Final Answer:
Using parentheses with continue causes a syntax error -> Option CQuick Check:
continue has no parentheses in Go [OK]
- Writing continue() instead of continue
- Confusing continue with break
- Misreading loop condition as error
for loop and continue. Which code snippet correctly achieves this?Solution
Step 1: Understand the goal
We want to skip printing numbers divisible by 3 and print all others from 1 to 10.Step 2: Analyze each option
for i := 1; i <= 10; i++ { if i % 3 == 0 { continue } fmt.Println(i) } skips printing multiples of 3 using continue correctly. for i := 1; i <= 10; i++ { if i % 3 != 0 { continue } fmt.Println(i) } skips non-multiples, printing only multiples. for i := 1; i <= 10; i++ { if i % 3 == 0 { break } fmt.Println(i) } breaks the loop early. for i := 1; i <= 10; i++ { if i % 3 == 0 { fmt.Println(i) continue } } prints multiples and skips others incorrectly.Final Answer:
for i := 1; i <= 10; i++ { if i % 3 == 0 { continue } fmt.Println(i) } -> Option BQuick Check:
continue skips multiples of 3, print others [OK]
- Using break instead of continue
- Reversing the condition logic
- Printing inside the continue block
Ready to prove it?
Pass the test and this pattern moves unknown
