Bootstrap FreeKB - Python (Scripting) - Move onto the next item in a loop using continue
Python (Scripting) - Move onto the next item in a loop using continue

Updated:   |  Python (Scripting) articles

  • break is used to break out of a loop
  • continue is used to move onto the next item in a loop

Let's say you have the following to loop through the items in the fruits list.

#!/usr/bin/python

fruits = ["apple", "banana", "orange", "grapes"]

for item in fruits:
  print(item)

 

Running this script will return the following. Each value in the list is printed.

apple
banana
orange
grapes

 

In this example, continue is used to move onto the next item in the fruits list.

#!/usr/bin/python

fruits = ["apple", "banana", "orange", "grapes"]

for item in fruits:
  if item == 'orange':
    continue
  print(item)

 

In this example, orange will not be printed.

apple
banana
grapes

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter a94db3 in the box below so that we can be sure you are a human.