0% found this document useful (0 votes)
7 views21 pages

Break Continue and Pass in Python

The document explains the use of 'break' and 'continue' statements in Python, which modify the flow of loops. The 'break' statement terminates the loop, while 'continue' skips the current iteration and proceeds to the next. Additionally, it introduces the 'pass' statement as a placeholder for future code implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views21 pages

Break Continue and Pass in Python

The document explains the use of 'break' and 'continue' statements in Python, which modify the flow of loops. The 'break' statement terminates the loop, while 'continue' skips the current iteration and proceeds to the next. Additionally, it introduces the 'pass' statement as a placeholder for future code implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Break and Continue in Python

The use of break and continue in Python

• In Python, break and continue statements can


alter the flow of a normal loop.
• Loops iterate over a block of code until the
test expression is false
• but sometimes we wish to terminate the
current iteration or even the whole loop
without checking test expression.
• The break and continue statements are used
in these cases.
Python break statement

• The break statement terminates the loop


containing it.
• Control of the program flows to the statement
immediately after the body of the loop.
• If the break statement is inside a nested loop
(loop inside another loop), the break statement
will terminate the innermost loop.
• Syntax of break
break
Flowchart of break
working of break statement
Example: Python break

• # Use of break statement inside the loop


• for val in "string":
• if val == "i":
• break
• print(val)
• print("The end")
Python continue statement

• The continue statement is used to skip the rest


of the code inside a loop for the current
iteration only.
• Loop does not terminate but continues on
with the next iteration.
• Syntax of Continue
• continue
Flowchart of continue
working of the continue statement in for and
while loop is shown below.
Example: Python continue

• for val in "string":


• if val == "i":
• continue
• print(val)
• print("The end")
Pass statement in Python

• In Python, pass is a null statement.


• The interpreter does not ignore a pass statement,
but nothing happens and the statement results
into no operation.
• The pass statement is useful when you don’t write
the implementation of a function but you want to
implement it in the future.

So, to avoid compilation errors, you can use


the pass statement.
Working
Syntax
pass
Writing a function with no body

• def addition(num1, num2):


• # Implementation will go here
• pass # Pass statement
• addition(2, 2)
Ignoring an if statement implementation

• def display(number):

• if number is 2:
• pass # Pass statement
• else:
• print ("Number: ", number)

• display(2)
• display(3)
•Examples
The working example using break
statement is as shown below:
• my_list = ['Siya', 'Tiya', 'Guru', 'Daksh', 'Riya', 'Guru']
• for i in range(len(my_list)):
• print(my_list[i])
• if my_list[i] == 'Guru':
• print('Found the name Guru')
• break
• print('After break statement')
• print('Loop is Terminated')
Example: Break statement inside while-loop

my_list = ['Siya', 'Tiya', 'Guru', 'Daksh', 'Riya', 'Guru']


• i=0
• while True:
• print(my_list[i])
• if (my_list[i] == 'Guru'):
• print('Found the name Guru')
• break
• print('After break statement')
• i += 1
• print('After while-loop exit')
Example: Break Statement inside nested loops

• for i in range(4):
• for j in range(4):
• if j==2:
• break
• print("The number is ",i,j);
Example : Continue inside for-loop

• for i in range(10):
• if i == 7:
• continue
• print("The Number is :" , i)
Example : Continue inside while-loop

• i=0
• while i <= 10:
• if i == 7:
• i += 1
• continue
• print("The Number is :" , i)
• i += 1
Example: pass statement inside a function

• def my_func():
• print('pass inside function')
• pass
• my_func()

You might also like