Table of contents
1.
Introduction
2.
Types of Jump Statements in Python
3.
Syntax of Jump Statements in Python
4.
Break Statement Syntax
4.1.
Python
5.
Continue Statement Syntax
5.1.
Python
6.
Return Statement Syntax
6.1.
Python
7.
Examples of Using Jump Statements in Python
7.1.
Example of Break Statement
7.2.
Python
8.
Example of Continue Statement
8.1.
Python
9.
Example of Return Statement
9.1.
Python
10.
Frequently Asked Questions
10.1.
What happens if I use a break statement outside of a loop?
10.2.
Can continue be used in a try block within loops?
10.3.
Is it necessary to have a return statement at the end of every function in Python?
11.
Conclusion
Last Updated: May 8, 2024
Easy

Jump Statement in Python

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Jump statements in Python are used to change the normal flow of a program. They allow you to skip over certain parts of the code or repeat specific sections based on certain conditions. 

Jump Statement in Python

In this article, we will learn the different types of jump statements in Python, their syntax & examples to help you understand how they work.

Types of Jump Statements in Python

In Python, there are three main types of jump statements that help in controlling program flow. These are break, continue, and return. Each serves a unique function, allowing a programmer to manage the execution of loops and functions more effectively.

  • Break Statement: The break statement is used to exit a loop prematurely when a certain condition is met. It's commonly used in while and for loops. When Python encounters a break, it immediately terminates the loop's execution and proceeds with the next line of code outside the loop.
     
  • Continue Statement: The continue statement skips the current iteration of the loop and moves to the next iteration. This is useful when you want to skip specific parts of a loop but continue running the loop. Unlike break, continue does not terminate the loop entirely; it just skips the remainder of the loop body, for the current iteration.
     
  • Return Statement: The return statement is used in functions to send a result back to the caller and terminate the function's execution. If a function reaches a return statement, it will stop executing immediately and send the return value back to where the function was called.

Syntax of Jump Statements in Python

Here, we will discuss how to properly use break, continue, and return statements with examples to clear their usage.

Break Statement Syntax

The break statement is straightforward to use. It is placed inside the body of a loop, typically after a conditional statement (if). Here’s a basic example:

  • Python

Python

for number in range(10):

   if number == 5:

       break

   print(number)
You can also try this code with Online Python Compiler
Run Code

Output

0
1
2
3
4


In this example, the loop will print numbers from 0 to 4. As soon as number equals 5, the break statement is executed, which stops the loop, and no more numbers are printed.

Continue Statement Syntax

The continue statement also follows a simple syntax. It is used inside a loop, generally after an if statement to skip the current iteration. Here’s how you can use it:

  • Python

Python

for number in range(10):

   if number % 2 == 0:

       continue

   print(number)
You can also try this code with Online Python Compiler
Run Code

Output

1
3
5
7
9


This code will print only the odd numbers between 0 and 9. Whenever number is even, the continue statement is executed, skipping the print statement for that iteration.

Return Statement Syntax

The return statement is used within a function to exit the function and return a value. It can return a specific value or no value at all (returning None). Here is an example of each:

  • Python

Python

def sum_two_numbers(a, b):

   return a + b

result = sum_two_numbers(5, 3)

print(result)
You can also try this code with Online Python Compiler
Run Code

Output:

8


In this function, return a + b sends the sum of a and b back to the caller.

def print_message():
    return
print_message()  # Returns None and exits the function


Here, the function print_message() does nothing other than returning None, effectively just showing how a return statement can be used to exit a function.

Examples of Using Jump Statements in Python

Now, let’s look at some examples that shows how break, continue, and return can be effectively used in different programming situations.

Example of Break Statement

Imagine you are writing a program that needs to process a list of usernames until it finds a specific user and then stops. Here's how you might use the break statement to achieve this:

  • Python

Python

usernames = ["alice", "bob", "charlie", "dave", "eve"]

target_user = "dave"

for user in usernames:

   if user == target_user:

       print(f"User {target_user} found!")

       break

   print(f"Checking user {user}...")
You can also try this code with Online Python Compiler
Run Code

Output

Checking user alice...
Checking user bob...
Checking user charlie...
User dave found!


In this code, the loop iterates through the usernames list. When it finds "dave", it prints a message, and the break statement stops the loop immediately.

Example of Continue Statement

Now, let's say you need to print all numbers from 1 to 10, skipping any numbers that are divisible by 3. The continue statement can be used as follows:

  • Python

Python

for number in range(1, 11):

   if number % 3 == 0:

       continue

   print(number)
You can also try this code with Online Python Compiler
Run Code

Output

1
2
4
5
7
8
10


This loop checks each number; if the number is divisible by 3, the continue statement skips the print function for that iteration, and the loop continues with the next number.

Example of Return Statement

Consider a function that checks if a number is prime. If the number is not prime, the function returns False immediately; otherwise, it returns True:

  • Python

Python

def is_prime(num):

   if num < 2:

       return False

   for i in range(2, int(num**0.5) + 1):

       if num % i == 0:

           return False

   return True

print(is_prime(11)) 

print(is_prime(4))
You can also try this code with Online Python Compiler
Run Code

Output: 

True
False


This function uses the return statement to send a boolean value back to the caller, stopping the function execution once a result is determined.

Frequently Asked Questions

What happens if I use a break statement outside of a loop?

Using a break statement outside of a loop results in a SyntaxError because break is only meant to be used within looping constructs like for or while.

Can continue be used in a try block within loops?

Yes, you can use continue in a try block within loops. However, ensure it aligns with the logic of your error handling to avoid unintended behaviors.

Is it necessary to have a return statement at the end of every function in Python?

No, it's not mandatory to have a return statement at the end of a function. If no return statement is used, the function will return None by default.

Conclusion

In this article, we have learned about the different types of jump statements in Python — break, continue, and return. We discussed their syntax & saw examples of how they control the flow of execution within a program. Understanding these statements allows you to write more efficient & clearer code, especially when managing complex loops or when decisions need to be made about exiting a function. These tools are fundamental for any Python programmer aiming to enhance their coding effectiveness and readability.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass