Python Break statement

Python break statement is used when you need to break the current loop completely, it mean no further iterations will be done for the current loop.

Consider below example, when the value of counter becomes 5, the if condition is True, which triggers the break statement, hence, no further iterations are carried out, the loop is broken or stopped, hence values only till 4 are printed.

Sample Output:

Python break statement
Python break statement

When there are nested loops, then the loop where break statement is called, that loop is stopped. In this example shown below, every time the character ‘c’ is encountered, the break statement executes, hence the rest of the inner loop doesn’t execute and the control moves to outer loop.

Sample Output:

Python Break Statement inside a nested loop
Python Break Statement inside a nested loop

Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!

Leave a Reply!

Your email address will not be published. Required fields are marked *