Thursday, December 26, 2024
Google search engine
HomeLanguagesbreak, continue and pass in Python

break, continue and pass in Python

Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from their normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements:

Break Statement in Python

The break statement in Python is used to terminate the loop or statement in which it is present. After that, the control will pass to the statements that are present after the break statement, if available. If the break statement is present in the nested loop, then it terminates only those loops which contain the break statement. 

Syntax of Break Statement

The break statement in Python has the following syntax:

for / while loop:
# statement(s)
if condition:
break
# statement(s)
# loop end

Working on Python Break Statement

The working of the break statement in Python is depicted in the following flowchart:

Working of Python Break Statement

Working of Python Break Statement

Example:

In this example, we will see the usage of break statements with Python loops and strings. First, we will use the break statement with for loops to exit out of the for loop when specific characters are encountered in the string. Then we will perform the same operation but with the while loop.

Python3




# Python program to demonstrate
# break statement
  
s = 'neveropen'
# Using for loop
for letter in s:
  
    print(letter)
    # break the loop as soon it sees 'e'
    # or 's'
    if letter == 'e' or letter == 's':
        break
  
print("Out of for loop")
print()
  
i = 0
  
# Using while loop
while True:
    print(s[i])
  
    # break the loop as soon it sees 'e'
    # or 's'
    if s[i] == 'e' or s[i] == 's':
        break
    i += 1
  
print("Out of while loop")


Output:

g
e
Out of for loop

g
e
Out of while loop

Example:

In this example, we will see the usage of break statements with nested for loops in Python. If the break statement is declared within the inner for loop, then the control comes out of only that loop. The outer for loops still continues to work.

Python3




# Python program to demonstrate
# break statement with nested
# for loop
  
# first for loop
for i in range(1, 5):
    
    # second for loop
    for j in range(2, 6):
        
        # break the loop if
        # j is divisible by i
        if j%i == 0:
            break
              
        print(i, " ", j)


Output:

3   2
4 2
4 3

Continue Statement in Python

Continue is also a loop control statement just like the break statement. continue statement is opposite to that of the break statement, instead of terminating the loop, it forces to execute the next iteration of the loop. As the name suggests the continue statement forces the loop to continue or execute the next iteration. When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped and the next iteration of the loop will begin.

Syntax of Continue Statement

The continue statement in Python has the following syntax:

for / while loop:
# statement(s)
if condition:
continue
# statement(s)

Working of Python Continue Statement

The working of the continue statement in Python is depicted in the following flowchart:

Working of Python Continue Statement

Working of Python Continue Statement

Example:

In this example, we will use Python continue statement with for loop to iterate through a range of numbers and to continue to the next iteration without performing the operation on that particular element when some condition is met.

Python3




# Python program to
# demonstrate continue
# statement
  
# loop from 1 to 10
for i in range(1, 11):
  
    # If i is equals to 6,
    # continue to next iteration
    # without printing
    if i == 6:
        continue
    else:
        # otherwise print the value
        # of i
        print(i, end = " ")


Output:

1 2 3 4 5 7 8 9 10 

Pass Statement in Python

As the name suggests pass statement simply does nothing. The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. It is like a null operation, as nothing will happen if it is executed. Pass statements can also be used for writing empty loops. Pass is also used for empty control statements, functions, and classes.

Syntax of Pass Statement

The pass statement in Python has the following syntax:

function/ condition / loop:
pass

Example: 

In this example, we will use the pass statement with an empty for loop and an empty Python function. We just declared a function and write the pass statement in it. When we try to call this function, it will execute and not generate an error.

Then we use the pass statement with an if condition within a for loop. When the value of “i” becomes equal to ‘k’, the pass statement did nothing, and hence the letter ‘k’ is printed.

Python3




# Python program to demonstrate
# pass statement
  
s = "Lazyroar"
  
# Empty loop
for i in s:
    # No error will be raised
    pass
  
# Empty function
def fun():
    pass
  
# No error will be raised
fun()
  
# Pass statement
for i in s:
    if i == 'k':
        print('Pass executed')
        pass
    print(i)


Output:

g
e
e
Pass executed
k
s

RELATED ARTICLES

Most Popular

Recent Comments