Continue

Comprehensive study notes, diagrams, and exam preparation for Continue.

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, and do-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 continue statement 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 i becomes 3, the continue statement is executed.
  • The print(i) statement is skipped for that iteration.
  • The loop continues with 4 and 5.

Important behavior:

  • In a for loop, continue skips directly to the next cycle.
  • In a while loop, continue skips to the condition check again, so the loop variable must be updated properly before continue, 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

  • continue and break are both loop control statements, but they behave differently.
  • continue skips only one iteration.
  • break stops 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 break when the loop should stop forever.
  • Use continue when 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 continue depends slightly on the type of loop being used.
  • It is most commonly used in for and while loops, and in some languages also in do-while loops.

Example in a while loop:

i = 0
while i < 5:
    i += 1
    if i == 2:
        continue
    print(i)

Output:

1
3
4
5

Explanation:

  • i is incremented before the continue.
  • When i == 2, print(i) is skipped.
  • If i were not incremented before continue, 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, continue is 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 → printed
  • n = 2 → even → skipped
  • n = 3 → not even → printed
  • n = 4 → even → skipped
  • n = 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 if statements, programmers can use continue to 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

  • continue skips 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