Python Continue Statement with Examples

In Python, the continue statement serves the purpose of bypassing the remaining code within a loop for the current iteration, allowing the program to move directly to the next iteration. This feature is particularly beneficial when particular conditions should be disregarded without ending or breaking out of the entire loop. The continue statement can be utilized in both for and while loops, enhancing both the efficiency and clarity of the code.

Syntax of the Continue Statement:

The structure of the Continue statement in Python is outlined below:

Example

# jump statement

continue;

In Python, the execution of the continue statement allows the loop to bypass the remaining code within the current iteration, swiftly proceeding to the subsequent iteration. When dealing with nested loops, it is important to note that the continue statement influences only the innermost loop.

Simple Python Continue Statement Example

Let us examine a straightforward illustration to grasp the functionality of the continue statement:

Example

Example

# Loop through numbers from 1 to 10

for num in range(1, 11):

    if num % 2 == 0:

        continue  # Skip the rest of the loop for even numbers

    print(num)

Output:

Output

1

3

5

7

9

Explanation:

In the preceding example, the continue statement is invoked for each value referred to as num, resulting in the program bypassing the print(num) function and proceeding to the subsequent iteration.

Flowchart of the continue Statement in Python

Presented below is the flowchart illustrating the functionality of the continue statement in Python:

Clarification of the Flowchart Pertaining to the Python Continue Statement:

Step 1: Initially, the loop starts.

Step 2: The condition of the loop is checked.

Step 3: Upon meeting the 'continue' condition:

  • It bypasses the remaining items in the loop.
  • Subsequently, it advances to the next iteration.

Step 4: In all other cases, the remainder of the loop's body will be executed.

Step 5: This entire sequence is reiterated until the loop concludes.

Different Examples of the continue Statement

Now, let's explore additional examples that demonstrate the utilization of the continue statement in Python.

Example 1: Skipping Specific Values

To illustrate the method of bypassing a particular value in Python, we will utilize the continue statement in the following example.

Example

Example

# here we are iterating through the characters in the string

for char in "Python Programming":

  # skipping the character 'o'

  if char == 'o':

    continue

  # printing the characters

  print(char, end='')

Output:

Output

Pythn Prgramming

Explanation:

In this case, the character 'o' is omitted each time it occurs within the string.

Example 2: Skipping Iterations in a while Loop

Let us consider an illustration to show how to bypass iterations within a while loop by utilizing the continue statement.

Example

Example

# initializing value of x as 0

x = 0

# using the while loop

while x < 10:

  # incrementing the value of x upto 10

  x += 1

  if x == 5:

    continue  # Skip printing when x is 5

  # printing the value of x

  print(x)

Output:

Output

1

2

3

4

6

7

8

9

10

Explanation:

When the value of x is 5, the continue statement halts the execution of print(x), causing the loop to move on to the subsequent iteration.

Example 3: Skipping Negative Numbers in a List

To illustrate the method of bypassing negative numbers within a list, we will utilize the continue statement in our example.

Example

Example

# list of numbers

numbers = [10, -3, 5, -8, 7]

# iterating through the elements of the list

for num in numbers:

  # skipping a number less than 0

  if num < 0:

    continue

  # printing the numbers

  print(num)

Output:

Output

10

5

7

Explanation:

In this illustration, negative values are ignored, and solely positive values are displayed.

Example 4: Skipping Certain Words in a Sentence

Let’s consider an example to illustrate how to bypass specific words in a sentence by utilizing the continue statement.

Example

Example

# given sentence

sentence = "Python learn from a ExampleTech"

# list of words to skip

words_to_skip = ["from", "a"]

# using for-loop

for word in sentence.split():

    if word in words_to_skip:

        continue    # continue statement to skip the words from given list

    print(word, end=' ')

Output:

Output

Python learn ExampleTech

Explanation:

In this instance, the terms "from" and "a" are omitted during the output of the remaining parts of the statement.

Example 5: Skipping Multiples of 3 in a Range

To illustrate how to bypass multiples of 3 within a specified range utilizing the continue statement, let’s consider an example.

Example

Example

# for loop to iterate through 1 to 20

for num in range(1, 20):

  # skipping the multiples of 3

  if num % 3 == 0:

    continue

  # printing the numbers

  print(num, end=' ')

Output:

Output

1 2 4 5 7 8 10 11 13 14 16 17 19

Explanation:

This illustration omits numbers that are divisible by 3 while outputting all others.

Example 6: Skipping Empty Strings in a List

Example

Example

# here is the list of words

words = ["apple", "", "banana", "cherry", ""]

# using a for-loop to iterate through the words in the list

for word in words:

  # skipping the empty string from the list

  if word == "":

    continue

  # printing the words from the list

  print(word)

Output:

Output

apple

banana

cherry

Advantages of Python Continue Statement

There are several advantages of using the Continue Statement in Python. Let's see some of them:

  • Skipping Unwanted Iterations: If there's a need to omit some value(s) in the loop, repeating sequences while the remainder of the iterations are still executed, the loop can be used.
  • Avoiding Nested if Conditions: Unwanted nested if statements are easily removed, and code is made less complex with a single continue statement. Controlling the flow of a program with continue is airy.
  • Efficient Filtering: In most cases where data is being processed through lists or iterations, unwanted values can be bypassed via continue.
  • Performance: The term continues, in regard to loops of computation or operation, moves the remaining parts of the loop of calculation to the next iteration; thus, in cases where particular operations are not needed, continuing assists in bringing forth suitable efficiency when it comes to value or return.
  • Specific Conditions: If there's a request to omit an operation on certain conditions without getting out of the loop, continuing is the best option available.
  • Conclusion

In Python, the "continue" statement allows you to bypass a particular loop iteration without terminating the entire iteration sequence. This feature is particularly useful when there are specific conditions that need to be ignored while still processing the remaining elements within a sequence. By implementing this code structure, the loops become more straightforward, enhancing readability and ultimately improving the program's overall efficiency.

Python Continue Statement - FAQs

1. What is the difference between break and continue?

The break statement terminates the loop entirely, while the continue statement merely bypasses the current iteration of the loop and proceeds to the subsequent one.

2. Can we use continue outside of Loops?

Regrettably, if utilized outside of a for or while loop, the continue statement will result in a syntax error, as it is exclusively applicable within these types of loops.

3. Can continue statement be used in Nested Loops?

Indeed, utilizing the continue statement will solely impact the loop defined within its own scope. Therefore, when a continue statement is employed in an inner loop, it will merely bypass the ongoing iteration of that specific inner loop. Meanwhile, the outer loop will proceed with its execution in the usual manner.

4. How does continue statement impact loop performance?

When dealing with extensive data sets, utilizing the command continue can enhance the efficiency of the loop by bypassing redundant calculations from the most recent iteration.

5. Can we use continue statement in a Try-Except block?

Provided that a loop is present, the continue statement can also be utilized within a try-except block.

6. What happens when continue statement is the last statement in a loop?

Any subsequent code that was intended to run after this will be disregarded during the current iteration, causing the loop to advance directly to the next iteration without delay.

Input Required

This code uses input(). Please provide values below: