Report this

What is the reason for this report?

how can i use continue in python

Posted on May 29, 2021

i know what continue function is used for but i have trouble in its format or how to use it and when.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi @aarushgambhir2007,

The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.

Basically, when you want to stop an itteration in a for loop for whatever reasons but want to continue with the said loop you use continue. Here are two examples:

#!/usr/bin/python

for letter in 'Python':     # First Example
   if letter == 'h':
      continue
   print 'Current Letter :', letter

var = 10                    # Second Example
while var > 0:              
   var = var -1
   if var == 5:
      continue
   print 'Current variable value :', var
print "Good bye!"

Regards, KFSys

Another example and a more detailed explanation:

The continue statement in Python is a control flow statement that is used inside loops. When Python encounters a continue statement in a loop, it immediately stops the current iteration and proceeds to the next iteration of the loop. This means that any code following the continue statement in the loop body will be skipped for the current iteration.

Here’s how to correctly use continue in a loop:

Example Usage in a for Loop

Suppose you have a list of numbers and you want to print only the odd numbers. You can use continue to skip even numbers.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for number in numbers:
    if number % 2 == 0:
        continue  # Skip the rest of the loop for even numbers
    print(number)  # This line will be executed only for odd numbers

In this example, any even number will cause the loop to execute the continue statement, which skips the print statement for that number.

Example Usage in a while Loop

Let’s say you are reading some data until a sentinel value (‘stop’) is encountered, but you want to skip processing any empty strings or strings that contain only whitespace.

while True:
    data = input("Enter data (type 'stop' to finish): ")
    if data.strip() == "":
        continue  # Skip the iteration if the input is empty or whitespace
    if data == "stop":
        break  # Exit the loop if 'stop' is entered
    print("Processing:", data)

Here, if the user enters an empty string or a string that contains only whitespace, continue is called, which skips the current loop iteration, thus print("Processing:", data) is not executed. If the user enters ‘stop’, the loop exits.

When to Use continue

  1. To avoid deeply nested conditions: continue can simplify your code by handling special cases early in the loop and reducing the level of nesting.
  2. To skip processing: It’s useful when there are certain conditions under which you want to skip the rest of the loop’s code for that particular iteration.
  3. To enhance readability: When used appropriately, continue can make it clearer that certain iterations of the loop are intentionally being skipped under specific conditions.

Remember, while continue can be helpful to improve the clarity of your code by reducing the need for deeper nesting of conditions, overuse or misuse can lead to code that’s harder to follow, especially in complex loops. Use it judiciously to make sure your code remains readable and maintainable.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.