1. Home
  2. Docs
  3. Programming with Python
  4. Control Statements
  5. Jumping Statements

Jumping Statements

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

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 """

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 """

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 """

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *