Saturday, November 16, 2024
Google search engine
HomeLanguagesJump Statements in Python

Jump Statements in Python

In any programming language, a command written by the programmer for the computer to act is known as a statement. In simple words, a statement can be thought of as an instruction that programmers give to the computer and upon receiving them, the computer acts accordingly.

There are various types of statements in programming and one such type of statement is known as Jump Statement. So, this article contains what a jump statement is, its types, its syntax, and a few programs based on them.

Jump Statement: As the name suggests, a jump statement is used to break the normal flow of the program and jump onto a specific line of code in the program if the specific condition is true. In simple words, it transfers the execution control of the program to another statement if a specific condition associated becomes true. As we proceed further you will get a better understanding of this concept.

Types of Jump Statements:

  •  Break: As the name suggests, a break statement is used to break or stop a flow control. This is generally used in a loop. A break statement is used in a loop in such a way, that when a particular condition becomes true, the break statement is executed and we come out of the loop immediately at that moment. 

Syntax:

Loop{
    Condition:
        break
    }

Example:

Python3




# for loop traversing from 1 to 4
for i in range(1, 5):
  # If this condition becomes true break will execute
    if(i == 3):
     # Break statement will terminate the entire loop
        break
    print(i)


Output

1
2
  • Continue: The continue statement is somewhat similar to the break statement but there is one significant difference between them.  A break statement terminates the entire loop but the continue statement skips only the current iteration and continues with the rest steps. So, if we use the continue statement in a loop, it will only skip one iteration when the associated condition is true and then continue the rest of the loop unlike the break statement. 

Syntax:

while True:
    ...
    if x == 100:
        continue
    print(x)

Example:

Python3




# for loop traversing from 1 to 4
for i in range(1, 5):
  # If this condition becomes true continue will execute
    if(i == 2):
      # Continue statement will skip the iteration when i=2 and continue the rest of the loop
        continue
    print(i)


Output

1
3
4
  •  Pass: The pass statement is an interesting statement, as when it gets executed nothing happens. It plays the role of a placeholder. This means that, if we want to write a piece of code over there in the near future but we cannot keep that space empty, then we can use the pass statement. In simple words, it is used to avoid getting an error from keeping an empty space.

Syntax:

def function: 
pass

Example:

Python3




# Traversing through every character of the string
for alphabet in 'Legends':
         # If alphabet='g' program will do nothing and won't print the letter
    if(alphabet == 'g'):
        pass
    else:
        print(alphabet)


Output

L
e
e
n
d
s
  • return: A return statement is also one type of jump statement. “return” statement terminates a function and returns a value to the calling function. That means the return statement is overall used to invoke a function so that the passed statements can be executed.

Syntax:

def fun():
  statements
  .
  .
  return [expression]

Python3




# Python program to demonstrate the
# return statement
 
# Function to multiply the two numbers
# x and y
def multiply(x, y):
 
    # returning the multiplication
    # of x and y
    return x * y
 
# Driver Code
result = multiply(3, 5)
 
print(result)


Output

15
RELATED ARTICLES

Most Popular

Recent Comments