Question

how can i use continue in python

Posted May 29, 2021 69 views
PythonDevelopmentProgramming Project

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

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.

×
Submit an Answer
1 answer

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