Python

Jump Statement in Python

Click for All Topics

Jump Statements in Python

Jump statements in programming are commands that give you special control over how your code works. They let you decide when to stop loops when to skip to the next iteration, or when to leave a function and give a result back. They are inbuilt in python and we don’t have to import them.

 

Types of jump statements:

  • Break
  • Continue
  • Pass
  • Return

1. Break:

It’s like a stop button for loops which means when we use this the loop will immediately end/terminate. We can use this with for and while of loops.

 

Example:

1.Using break with for loop:

				
					# Welcome to edSlash learning.
for i in range(5):
	if i == 3:
		print("Reached 3, breaking the loop.")
		break
print("Iteration:", i)

# Output -
# Reached 3, breaking the loop.
# Iteration: 3
				
			
				
					# Welcome to edSlash learning
digits=[1,2,3,4,5,6,7,8,9,10]
for num in digits:
    if num==7:
        break
    print(num)

# Output
# 1
# 2
# 3
# 4
# 5
# 6
				
			

2. Using break with  while loop:

				
					count = 0
while count < 5:
	if count == 3:
		print("Count reached 3, stopping the loop.")
		break
	print("Count:", count)
	count += 1

# Output -
# Count: 0
# Count: 1
# Count: 2
# Count reached 3, stopping the loop.
				
			

3. Using break in nested loop:

A. Using break with for loop:

				
					# Breaking out of neested for loop.
for i in range(3):  
    for j in range(3):
        if i*j=4:
            break 
        print(i*j)
# Output-
# 0 0
# 0 1
# 0 2
# 1 0
# 1 1
				
			
				
					# Example of a nested for loop with 'break'
for i in range(3):
	print("Outer loop iteration:", i)
	for j in range(3):
		print("Inner loop iteration:", j)
		if i == 1 and j == 1:
			print("Breaking inner loop.")
			break

# Output -
# Outer loop iteration: 0
# Inner loop iteration: 0
# Inner loop iteration: 1
# Inner loop iteration: 2
# Outer loop iteration: 1
# Inner loop iteration: 0
# Inner loop iteration: 1
# Breaking inner loop.
# Outer loop iteration: 2
# Inner loop iteration: 0
# Inner loop iteration: 1
# Inner loop iteration: 2
				
			

B. Using break with while loop:

				
					# Example of a while loop with 'break'
outer_count = 1
while outer_count <= 3:
	print("Outer loop iteration:", outer_count)
	inner_count = 1
	while inner_count <= 3:
		print("Inner loop iteration:", inner_count)
		if outer_count == 2 and inner_count == 2:
			break
		inner_count += 1
	outer_count += 1

# Output -
# Outer loop iteration: 1
# Inner loop iteration: 1
# Inner loop iteration: 2
# Inner loop iteration: 3
# Outer loop iteration: 2
# Inner loop iteration: 1
# Inner loop iteration: 2
# Outer loop iteration: 3
# Inner loop iteration: 1
# Inner loop iteration: 2
# Inner loop iteration: 3
				
			

2. Continue:

The continue statement is like a skip button for loops in programming. When encountered, it immediately jumps to the next iteration of the loop, skipping the rest of the current iteration’s code.

 

A. Using continue with while

				
					# Example of a while loop with 'continue'
count = 1
while count <= 10:
    if count % 2 == 0:
        count += 1
        continue
    print(count)
    count += 1
    
# Output-
# 1
# 3
# 5
# 7
# 9

				
			
				
					# Example of a while loop with 'continue'
count = 1
while count <= 5:
	if count == 3:
		print("Skipping count", count)
		count += 1
		continue
	print("Count:", count)
	count += 1

# Output -
# Count: 1
# Count: 2
# Skipping count 3
# Count: 4
# Count: 5
				
			

B. Using continue with for

				
					# Example of a for loop with 'continue'
names = ['Adam', 'Brendon', 'Chris', 'David', 'Ewen']
for name in names:
    if len(name) > 4:
        continue
    print(name)
# Output-
# Adam
# Ewen

				
			
				
					# Example of a nested for loop with 'continue'
for x in range(3):
	print("Outer loop iteration:", x)
	for y in range(3):
		if x == 1 and y == 1:
			print("Skipping inner loop iteration", y)
			continue
		print("Inner loop iteration:", y)

# Output -
# Outer loop iteration: 0
# Inner loop iteration: 0
# Inner loop iteration: 1
# Inner loop iteration: 2
# Outer loop iteration: 1
# Inner loop iteration: 0
# Skipping inner loop iteration 1
# Inner loop iteration: 2
# Outer loop iteration: 2
# Inner loop iteration: 0
# Inner loop iteration: 1
# Inner loop iteration: 2
				
			

3. Pass:

Its like a do nothing command. It’s used when you need a placeholder for a certain block of code but doesn’t want to add any actual instructions yet.

It helps you avoid syntax errors when a block is required but you’re not ready to add functionality.

A. Using pass with function

				
					# Empty function
def isfunction():
    pass
				
			

B. Using pass with for

				
					# Example of a for loop with 'pass'
for i in range(1, 6):
	pass
				
			

C. Using pass with while

				
					# Example of a while loop with 'pass'
count = 1
while count <= 5:
	pass
				
			

D. Using pass with conditional statement

				
					# Example of a conditional statement with 'pass'
if anycondition:
    pass
else:
				
			
4. Return:

“return” statement in python is used when user needs to exit from a function and needs to return a value to the calling function. 

				
					# Example of a return statement
def add_no(m,n):
    addition=m+n
    return addition
# calling the function
result=add_no(2,4)
print(result)

# Output- 
# 6