Use of continue in python

+4 votes
hey, I am confused with continue and break statement. Can anyone please tell me what is the practical use of "continue" statement in python?
Apr 7, 2018 in Python by code.reaper12
• 3,500 points
1,698 views

6 answers to this question.

+2 votes
Best answer

The break statement is used to "break" the loop. It means that when a break statement is executed, it skips the execution below it in the loop and exits the loop. 

Ex: 

for letter in 'HelloWorld':    
    if letter == 'W':
        break
    print 'Character is:', letter

Output for this code will be:

Character is: H

Character is: e

Character is: l

Character is: l

Character is: o

The continue statement is used to continue the loop by skipping the execution of code below it i.e., when a continue statement is executed, it skips executing the code below it but goes back to the loop and continues with the next cycle.

Ex:

for letter in 'Hello':    
    if letter == 'H':
        continue
    print 'Character is: ', letter

Output for this code will be:

Character is: e

Character is: l

Character is: l

Character is: o

answered Jul 16, 2018 by Omkar
• 69,220 points

selected Oct 12, 2018 by Omkar
+1 vote

Here's an simple example :

for letter in 'Django':    
    if letter == 'D':
        continue
    print 'Current Letter:', letter

Output will be :

Current Letter: j
Current Letter: a
Current Letter: n
Current Letter: g
Current Letter: o
answered Oct 12, 2018 by findingbugs
• 4,780 points
+1 vote
Usually the situation where continue is necessary/useful, is when you want to skip the remaining code in the loop and continue iteration.

I don't really believe it's necessary, since you can always use if statements to provide the same logic, but it might be useful to increase readability of code.
answered Oct 12, 2018 by abc
+1 vote
import random  

for i in range(20):  
    x = random.randint(-5,5)  
    if x == 0: continue  
    print 1/x  

continue is an extremely important control statement. The above code indicates a typical application, where the result of a division by zero can be avoided. I use it often when I need to store the output from programs, but dont want to store the output if the program has crashed. Note, to test the above example, replace the last statement with print 1/float(x), or you'll get zeros whenever there's a fraction, since randint returns an integer. I omitted it for clarity.

answered Oct 12, 2018 by rani
+1 vote

Suppose you need a check before the main code:

if precondition_fails(message): continue

''' main code here '''

Note you can do this after the main code was written without changing that code in anyway. If you diff the code, only the added line with "continue" will be highlighted since there are no spacing changes to the main code.

answered Oct 12, 2018 by Progba
+1 vote
def filter_out_colors(elements):
  colors = ['red', 'green']
  result = []
  for element in elements:
    if element in colors:
       continue # skip the element
    # You can do whatever here
    result.append(element)
  return result

  >>> filter_out_colors(['lemon', 'orange', 'red', 'pear'])
  ['lemon', 'orange', 'pear']
answered Oct 12, 2018 by nabo

Related Questions In Python

+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
4,881 views
0 votes
1 answer

What is the use of raw_input function in Python?

raw_input fuction is no longer available in ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
1,257 views
0 votes
1 answer

When to use %r instead of %s in Python? [duplicate]

The %s specifier converts the object using ...READ MORE

answered Aug 2, 2018 in Python by bug_seeker
• 15,510 points
1,164 views
0 votes
1 answer

What is the use of “assert” keyword in Python?

You can try the following in a ...READ MORE

answered Oct 15, 2018 in Python by Priyaj
• 58,020 points
1,062 views
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,070 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer

How to check latest change time of each file using glob in python?

You can get the changing time from ...READ MORE

answered Jan 2, 2019 in Python by Omkar
• 69,220 points
1,558 views
0 votes
3 answers

Python program to print occurrence of character in string

Try below code string = input("Enter a string: ...READ MORE

answered May 28, 2020 in Python by Anubhav
• 300 points
30,766 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP