Jumping statements are used to alter the normal flow of program execution in loops or conditional blocks.
- They help manage the execution flow by skipping iterations, exiting loops, or stopping the program.
Python provides three primary jumping statements:
- break Statement
- continue Statement
- pass Statement
1.) break Statement
The break statement is used to exit from a loop or a switch statement prematurely. It stops the execution of the loop or switch and transfers control to the next statement outside of the loop.
Example:
for i in range(10):
if i = = 5:
break # Exits the loop when i equals 5
print(i)
#output
"""0
1
2
3
4 """
2.) continue Statement
The continue statement is used to skip the current iteration of a loop and move on to the next iteration. It does not terminate the loop; it just skips the code below it for the current iteration.
Example:
for i in range(5):
if i = = 3:
continue # Skips when i equals 3
print(i)
#output
"""0
1
2
4 """
3.) pass statement
The pass statement does nothing and is used as a placeholder. It is useful when a block of code is syntactically required but no action is needed.
Example:
for i in range(5):
if i = = 3:
pass # Does nothing when i equals 3
print(i)
#output
"""0
1
2
3
4 """