Continue
Definition
The continue statement is a loop control statement that immediately stops executing the remaining code inside the current loop iteration and transfers control to the next iteration of the loop.
In simple terms:
- It does not end the loop.
- It only skips the current cycle.
- It is used inside loops such as
for,while, anddo-while.
Example in many languages:
if (condition) {
continue;
}
This means: if the condition becomes true, skip the rest of the statements in that loop iteration and go to the next iteration.
Main Content
1. First Concept
Loop Control and Flow Skipping
- The
continuestatement changes the normal flow of a loop by skipping all statements that appear after it in the current iteration. - It is used when a specific condition makes the remaining part of the loop unnecessary or undesirable for that iteration.
Example:
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1
2
4
5
Explanation:
- When
ibecomes3, thecontinuestatement is executed. - The
print(i)statement is skipped for that iteration. - The loop continues with
4and5.
Important behavior:
- In a
forloop,continueskips directly to the next cycle. - In a
whileloop,continueskips to the condition check again, so the loop variable must be updated properly beforecontinue, otherwise an infinite loop may occur.
Common use cases
- Skipping invalid entries in a data list.
- Ignoring specific numbers, characters, or records.
- Preventing execution of unnecessary code.
2. Second Concept
Difference Between continue and break
continueandbreakare both loop control statements, but they behave differently.continueskips only one iteration.breakstops the loop completely.
Comparison table:
| Statement | Effect |
|---|---|
continue |
Skips the current iteration and keeps looping |
break |
Ends the entire loop immediately |
Example:
for i in range(1, 6):
if i == 3:
break
print(i)
Output:
1
2
Now with continue:
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1
2
4
5
Key idea:
- Use
breakwhen the loop should stop forever. - Use
continuewhen only one iteration should be ignored.
Why this difference matters
- It helps in writing correct program logic.
- It avoids accidental stopping of loops.
- It improves readability when filtering or skipping items.
3. Third Concept
continue in Different Loop Types and Practical Use
- The behavior of
continuedepends slightly on the type of loop being used. - It is most commonly used in
forandwhileloops, and in some languages also indo-whileloops.
Example in a while loop:
i = 0
while i < 5:
i += 1
if i == 2:
continue
print(i)
Output:
1
3
4
5
Explanation:
iis incremented before thecontinue.- When
i == 2,print(i)is skipped. - If
iwere not incremented beforecontinue, the loop could repeat forever.
Practical examples:
- Skipping negative numbers in a list.
- Ignoring blank lines in file processing.
- Filtering out certain user inputs.
- Skipping weekends in date-based loops.
ASCII illustration of loop flow:
Start loop
|
v
Check condition
|
v
Current item meets skip condition?
| yes
v
continue -> go to next iteration
|
no
v
Execute remaining statements
|
v
Move to next iteration
This shows that continue acts like a shortcut that bypasses the remaining code in the loop body for that round.
Working / Process
1. The loop begins its current iteration
- The loop variable or current item is obtained.
- The program starts executing statements inside the loop body.
2. A condition is checked
- The program tests whether the current item meets a specific rule.
- If the rule is not met, the loop continues normally.
- If the rule is met,
continueis triggered.
3. The remaining statements are skipped
- The rest of the code inside the current loop body is not executed.
- Control moves immediately to the next iteration of the loop.
- The loop continues until its normal ending condition is reached.
Example process in a number loop:
for n in range(1, 6):
if n % 2 == 0:
continue
print(n)
Step-by-step:
n = 1→ not even → printedn = 2→ even → skippedn = 3→ not even → printedn = 4→ even → skippedn = 5→ not even → printed
Output:
1
3
5
This process is especially useful when working with filtering logic.
Advantages / Applications
Improves program efficiency
- Unnecessary instructions are skipped, which can reduce processing time in large loops.
Makes code cleaner and easier to read
- Instead of using many nested
ifstatements, programmers can usecontinueto skip unwanted cases early.
Useful for filtering and validation
- It helps ignore invalid data, unwanted values, or special conditions during iteration.
Examples of applications:
- Processing only even numbers in a sequence.
- Skipping empty strings in a list of words.
- Ignoring malformed records in a dataset.
- Skipping certain rows in a table or file.
- Avoiding calculations for values that do not meet a requirement.
Example:
names = ["Ana", "", "John", "", "Mira"]
for name in names:
if name == "":
continue
print(name)
Output:
Ana
John
Mira
Here, empty names are skipped automatically.
Summary
continueskips the rest of the current loop iteration.- It is used to avoid executing unwanted statements in loops.
- It is different from
break, which ends the loop entirely. - Important terms to remember:
continue, loop, iteration, skip, control flow