CodingBison

Python uses "continue" and "break" keywords to be more selective when running in a loop. A program can use "continue" keyword to skip some of the items while iterating through a sequence. Or, it can use "break" keyword to terminate the loop immediately once it finds a certain item. Thus, we can use "continue" to eliminate items and use "break" to select items.

Let us say, we need to print a sequence of items and in doing so we need to skip those items that do not meet a specific criteria, then we can do so with "continue". During iteration, whenever an item does not meet the specified criteria, we can skip that item and continue to the next one.

While "continue" allows us to eliminate items that do not meet a criteria, "break" allows us to select the very first item that meets the criteria and exit. We can use the "break" to look for a specific item and the moment we find it, we can exit out of the loop without even bothering to analyze the remaining items.

Note that when we break out of the loop (while-loop or for-loop), we exit the loop without running the remaining statements, including the "else:" clause of the loop.

For the case when we have multiple levels of inner loops within an outer loop, the "continue" statement simply skips the current item of the immediate loop that encloses the "continue" statement. Likewise, the "break" statement breaks out of the immediate loop that encloses the "break" statement.

Let us modify the previous example to see usage of these keywords. For the sake of simplicity, we use a "for" loop; these keywords apply equally to the whole loop as well, including the for variant. This example processes a list of wild-animals and searching for the "lion" value. It uses "continue" to eliminate all those items that do not equal this value.

 listCats = ["tiger", "lion", "mountain lion", "lion", "house-cat", "leopard"]

 totalLionCount = 0 
 for element in listCats:
     if element != "lion":
         print("Let us continue searching...")
         continue

     print("Found lion!")
     totalLionCount += 1 
 else:
     print("The list contains " + str(totalLionCount) + " lions")

Here is the output when we run the above example; we save it in file "continue-loop.py" and run it.

 [user@codingbison]$ python3 continue-loop.py 
 Let us continue searching...
 Found lion!
 Let us continue searching...
 Found lion!
 Let us continue searching...
 Let us continue searching...
 The list contains 2 lions
 [user@codingbison]$ 

Let us reuse the above example to understand the break keyword. In this example (provided below), we look for the value "mountain lion" and when we find it, we break out of the loop. Thus, the "break" allows us to be more selective -- even if the sequence contain multiple occurrences of a value, the break only looks at the first occurrence and ignores the rest!

 listCats = ["tiger", "lion", "mountain lion", "lion", "house-cat", "leopard"]

 for element in listCats:
     if element == "mountain lion":
         print("Found mountain lion. Let us break out of loop")
         break

     print("Searching for mountain lion...")
 else:
     print("Could not find a mountain lion")

When we run this example, we see that we break out the moment we found the value we are looking for. Also, note that with this, the "else:" clause does not get executed.

 [user@codingbison]$ python3 break-loop.py 
 Searching for mountain lion...
 Searching for mountain lion...
 Found mountain lion. Let us break out of loop
 [user@codingbison]$ 




comments powered by Disqus