Python Break
Python Break
Statement Description
break Terminate the current loop. Use the break statement to come out of the loop instantly.
continue Skip the current iteration of a loop and move to the next iteration
pass Do nothing. Ignore the condition in which it occurred and proceed to run the program as usual
The break and continue statements are part of a control flow statements that
helps you to understand the basics of Python.
For example, you are searching a specific email inside a file. You started
reading a file line by line using a loop. When you found an email, you can stop
the loop using the break statement.
We can use Python break statement in both for loop and while loop. It is
helpful to terminate the loop as soon as the condition is fulfilled instead of
doing the remaining iterations. It reduces execution time.
AD
Syntax of break:
break
In this example, we will iterate numbers from a list using a for loop, and if we
found a number greater than 100, we will break the loop.
Use the if condition to terminate the loop. If the condition evaluates to true,
then the loop will terminate. Else loop will continue to work until the main
loop condition is true.
AD
numbers = [10, 40, 120, 230]
for i in numbers:
if i > 100:
break
print('current number', i)
Output:
curret number 10
curret number 40
Note: As you can see in the output, we got numbers less than 100 because we
used the break statement inside the if condition (the number is greater than
100) to terminate the loop
We can use the break statement inside a while loop using the same approach.
Write a while loop to display each character from a string and if a character is
a space then terminate the loop.
Use the if condition to stop the while loop. If the current character is space
then the condition evaluates to true, then the break statement will execute and
the loop will terminate. Else loop will continue to work until the main loop
condition is true.
size = len(name)
i = 0
# iterate loop till the last character
while i < size:
# break loop if current character is space
if name[i].isspace():
break
# print current character
print(name[i], end=' ')
i = i + 1
AD
Flow chart of a break
statement
To terminate the nested loop, use a break statement inside the inner loop. Let’s
see the example.
In the following example, we have two loops, the outer loop, and the inner
loop. The outer for loop iterates the first 10 numbers using the range
() function, and the internal loop prints the multiplication table of each
number.
AD
But if the current number of both the outer loop and the inner loop is greater
than 5 then terminate the inner loop using the break statement.
To terminate the outside loop, use a break statement inside the outer loop.
Let’s see the example.
In the following example, we have two loops, the outer loop, and the inner
loop. The outer loop iterates the first 10 numbers, and the internal loop prints
the multiplication table of each number.
But if the current number of the outer loop is greater than 5 then terminate
the outer loop using the break statement.
AD
Continue Statement in Python
The continue statement skip the current iteration and move to the next
iteration. In Python, when the continue statement is encountered inside the
loop, it skips all the statements below it and immediately jumps to the next
iteration.
Syntax of continue:
continue
AD
In this example, we will iterate numbers from a list using a for loop and
calculate its square. If we found a number greater than 10, we will not
calculate its square and directly jump to the next number.
Use the if condition with the continue statement. If the condition evaluates to
true, then the loop will move to the next iteration.
Output:
Current Number is 2
Current Number is 11
Current Number is 7
Note: As you can see in the output, we got square of 2, 3, and 7, but the loop
ignored number 11 because we used the if condition to check if the number is
greater than ten, and the condition evaluated to true, then loop skipped
calculating the square of 11 and moved to the next number.
AD
• In the first iteration of the loop, 4 gets printed, and the condition i
> 10 is checked. Since the value of i is 2, the condition becomes
false.
• In the second iteration of the loop, 9 gets printed, and the
condition i > 10 is checked. Since the value of i is 9, the condition
becomes false.
• In the third iteration of the loop, the condition i > 10 becomes
true, and the continue statement skips the remaining statements
and moves to the next iteration of the loop
• In the second iteration of the loop, 49 gets printed, and the
condition i > 10 is checked. Since the value of i is 7, the condition
becomes false.
We can also use the continue statement inside a while loop. Let’s understand
this with an example.
Write a while loop to display each character from a string and if a character is
a space, then don’t display it and move to the next character.
Use the if condition with the continue statement to jump to the next iteration. If
the current character is space, then the condition evaluates to true, then
the continue statement will execute, and the loop will move to the next
iteration by skipping the remeaning body.
size = len(name)
i = -1
# iterate loop till the last character
while i < size - 1:
i = i + 1
# skip loop body if current character is space
if name[i].isspace():
continue
# print current character
print(name[i], end=' ')
AD
Output:
J e s a a
Continue Statement in Nested Loop
To skip the current iteration of the nested loop, use the continue statement
inside the body of the inner loop. Let’s see the example.
In the following example, we have the outer loop and the inner loop. The
outer loop iterates the first 10 numbers, and the internal loop prints the
multiplication table of each number.
But if the current number of the inner loop is equal to 5, then skip the current
iteration and move to the next iteration of the inner loop using
the continue statement.
To skip the current iteration of an outside loop, use the continue statement
inside the outer loop. Let’s see the example
In the following example, The outer loop iterates the first 10 numbers, and the
internal loop prints the multiplication table of each number.
But if the current number of the outer loop is even, then skip the current
iteration of the outer loop and move to the next iteration.
Note: If we skip the current iteration of an outer loop, the inner loop will not
be executed for that iteration because the inner loop is part of the body of an
outer loop.
A pass statement is a Python null statement. When the interpreter finds a pass
statement in the program, it returns no operation. Nothing happens when
the pass statement is executed.
Example
AD
Output