Python Break, Continue, Pass Statements

Break Statement

break statement is used in for/while loop structures to exit loop immediately. Suppose that you are searching for an item in a list. If you find the item you are searching for, you don’t need to continue iterating other items, so that you can use break statement.

for name in phoneDirectory:
if name == "John":
    print("John's number is found.")

Continue Statement

continue statement skips rest of the code in the current loop and continues with the next iteration. For instance, if you need only even numbers in a list, you can skip odd numbers using a continue statement.

for i in range(1,11):
    if i % 2 == 1:      # skips current loop if the number is odd.

Pass Statement

pass statement is a null operation, it does not execute anything. Pass statement can be used as a placeholder when a statement is required syntactically. For instance, you can create empty functions when you are designing new applications.

def new_function(args):
    pass       # pass statement does nothing.
               # However, without a pass statement,
               # this code will not compile.

You can use pass statements also in loops.

for name in phoneDirectory:
    if name == "John":
        pass                # Do nothing for now.
                            # This code will be updated later.