Lokang 

Python and MySQL

Break

In Python, the break statement is used to exit a loop prematurely, regardless of whether the loop condition has been met or not. This is particularly useful when you want to stop the loop based on a specific condition other than the one defined by the loop itself.

Here's an example of how the break statement can be used:

for i in range(1, 10):
   if i == 5:
       break  # Exit the loop when i equals 5
   print(i)

In this code snippet, the for loop iterates from 1 to 9. However, when i reaches 5, the break statement is executed, causing the loop to terminate immediately. Thus, the output of this code will be the numbers 1 through 4, because the print statement stops getting executed once i equals 5.

The break statement is commonly used in loops that search for items in a list or other data structures, where you might want to stop looping as soon as you find what you're looking for.

1
2
3
4