BREAK KEYWORD IN PYTHON | Python Break

Python Break

Now, we are going to study a very interesting and very useful keyword, which is the break keyword in python. As the name of the keyword says, the break keyword is going to be used to break out of the for loop, or a while loop. Usually, the loops iterate over a sequence, or till the expression evaluates to True. But sometimes in our programs, it might be required for us to break out of the loop, even if the loop was not going to terminate. In such situations, we can make use of the break keyword.

Let’s have a look at a simple program, which demonstrates the break keyword. In the below program, we have a list, which contains some numbers. In the list, we will iterate through different elements, and find if the number is divisible by 5. if we find a number that is divisible by 5, we will just break out of the loop. This example may not make complete sense, but still, it can make us understand the use of break keyword in our programs.

Python Break

As you can see, in the above program, we have a list, in which we have some numbers. Then we are iterating through each element in the list, and then we are checking that if the number is divisible by 5, and if the number is divisible by 5, then we are just breaking out of the loop. So, the output of the program comes out to be something like this –

12 not divisible by 5
29 not divisible by 5
3 not divisible by 5
31 not divisible by 5
Number divisible by 5 found! the number is 45
We are out of the loop!

As you can see, we were iterating through all the elements in the list, and then we were checking if the number is divisible by 5. When we found the number 45, which is divisible by 5, we just moved out of the loop, even if there were some other elements left in the list.

Let’s have a look at another program, which would get you a clearer idea, about using the break keyword. In the below program, we would simply search for some number in the list, which contains many numbers. Let’s have a look at the program first, and then we will try to explain it.

As you can see in the above program, we have a list, which contains some number, and then we have a variable searchkey, which is assigned the value to search for in the list. We iterate through the list, comparing if the current list element is equal to the searchkey. If the element is equal to the searchkey, we are just assigning True to the found variable, and then we are breaking out of the loop. Initially we have the found variable set to False. After the loop ends, we are checking if the value of found is True, then we would say that we found the value, and else we would say that we did not find the value. In the above situation, the key that we are searching for in the list, is present in the list. So, this is what the output looks like
Search Key 22 was found

As you can see in the output, we have the key found. You can try for some other key, which is not there in the list, and then you would get a different output. The thing is that in the above program, if we have found the element that we are searching for in the sequence, then it makes no sense in keeping on iterating, so we break out of the loop.

So, in such situations, when we need to break out of the loop, we can make use of the break keyword, and just move out of the loop. You can try the same thing with the while loop as well. We would make use of the break keyword in our python programs, as and when required. But remember that the break keyword is simply used to break out of the while loop, or the for loop.