Python | Break, Continue, and Pass
- Break: The break statement terminates the loop immediately and the control flows to the statement after the body of the loop.
- Continue: The continue statement skips the current iteration of the statement, and the control flows to the next iteration of the loop.
- Pass: The pass keyword in Python is generally used to fill up empty blocks.
The break statement is used inside the loop to exit out of the loop. In Python, when a break statement is encountered inside a loop, the loop is immediately terminated, and the control flows to the statement after the body of the loop.
for i in range(11): if i > 7: break print('current number', i)
The continue statement skips the current iteration and moves to the next iteration. In Python, when the continue statement is encountered inside the loop, it skips all the statements below it and immediately jumps to the next iteration.
for i in range(11): if i == 7: continue print('current number', i)
The pass statement is used as a placeholder for future code. It represents a null operation in Python. It is generally used for the purpose of filling up empty blocks of code which may execute during runtime but has yet to be written.
def myfunction(): pass