continue – Skips the current execution and continues with the next iteration of for & while loops.
Python for loop continues
Simple example code passes the continue after conditioning within loops.4
for var in "ABCD": if var == "C": print('Continue statement') continue print(var)
Printing range with Python Continue Statement
for i in range(1, 11): # Check 5 and skip it if i == 6: continue else: print(i, end=" ")
Output: 1 2 3 4 5 7 8 9 10
# First Example
for letter in 'Python': if letter == 'h': continue print 'Current Letter :', letter
# Second Example
var = 10 while var > 0: var = var -1 if var == 5: continue print 'Current variable value :', var print "Good bye!"
Quick Examples Using for Loop continue and break
courses=["java","python","pandas","sparks"] # Example 1 : Using continue in for loop for x in courses: if x=='pandas': continue print(x) # Example 2 : Using break in for loop for x in courses: print(x) if x == 'pandas': break # placed the print() after the break for x in courses: if x == 'pandas': break print(x) # Using break statement inside the nested loop ourses1=["java","python"] courses2=["pandas","java","python"] for x in courses1: for y in courses2: if x==y: break print(x ,y) # Using else block with a break statement for x in courses: print(x) if x == 'pandas': break else: print("Task finished")
Do comment if you have any doubts or suggestions on this Python for loop topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.