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
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
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
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
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
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
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 DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.