JUMP STATEMENTS

Looping allows a user to program and repeat tasks efficiently. In certain situations, when some particular condition occurs, a user may want to exit from a loop (come out of the loop forever) or skip some statements of the loop before continuing further in the loop. These requirements can be taken care of by use of break and continue control statements respectively. Python provides these jump statements as a tool to give more flexibility to the programmer to control logic of loops.

Jump statements in Python are used to change the normal flow of execution of a program. There are three types of jump statements in Python: break, continue, and pass.

 

1. break: The break statement is used to terminate the current loop prematurely. When a break statement is encountered inside a loop, the loop is immediately terminated, and program execution continues with the statement following the loop.

# Example of break statement

numbers = [1, 2, 3, 4, 5]

for num in numbers:
    if num == 3:
        break
    print(num)
    
# Output: 
# 1
# 2

 

In this example, we have a list of numbers and a for loop that iterates over each number in the list. When the loop encounters the number 3, the break statement is executed, and the loop is terminated. The program then continues with the statement following the loop.

 

2. continue: The continue statement is used to skip the current iteration of a loop and continue with the next iteration. When a continue statement is encountered inside a loop, the current iteration is terminated, and the loop proceeds with the next iteration.

# Example of continue statement

numbers = [1, 2, 3, 4, 5]

for num in numbers:
    if num == 3:
        continue
    print(num)
    
# Output: 
# 1
# 2
# 4
# 5

 

In this example, we have a list of numbers and a for loop that iterates over each number in the list. When the loop encounters the number 3, the continue statement is executed, and the current iteration is terminated. The loop then proceeds with the next iteration.

 

3. pass: The pass statement is used as a placeholder when a statement is required syntactically but no code needs to be executed. It is often used as a placeholder for functions or classes that will be implemented later.

# Example of pass statement

def my_function():
    pass
    
class MyClass:
    pass

In this example, we have a function and a class definition that both contain the pass statement. This indicates that the function and class will be implemented later, but we want to define them syntactically correct.

 

x = 10

if x > 5:
    pass
else:
    print("x is not greater than 5")

 

In this example, we use pass as a placeholder inside an if statement. If x is greater than 5, the pass statement is executed, and the program continues with the statement following the if block. If x is not greater than 5, the print statement is executed instead.

In this case, the pass statement is not necessary and could be removed without changing the program’s behavior. However, pass can be useful as a placeholder when you want to indicate that code will be added later or as a way to temporarily disable a block of code during development.

Copywrite © 2020-2024, CBSE Python,
All Rights Reserved
error: Content is protected !!