The continue and pass statements

While the break statement terminates, the entire loop continue statement will skip that step and the loop will continue thereafter.

Let's discuss with an example:

movies = ['P.S I Love You', 'Shutter Island', 'Les Miserables play', 'King's Speech', 'Forest Gump']

Here is the list of movies but Les Miserables is the name of a play and we would like to skip this name and want the program to print all the names of the movies:

movies = ["P.S I Love You", "Shutter Island", "Les Miserables Play", "King's Speech", "Forest Gump"] 

for each in movies:
if (each== "Les Miserables Play"):
continue
print each

The for loop iterates through each element of the list and the if block checks the condition for the occurrence of "Les Miserables Play"

Here, the continue statement just skipped "Les Miserables Play" and the loop continued to print the rest of the names of the movies.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.144.216