Python Break continue statement

The Python break and continue statements are some useful statements that can change utterly how our program behaves. Here we will discuss each of them in detail.


Python break

The Python break statement is used to break out of the iterating statement like for or while statement. Suppose, while iterating through for loop you have come across the element you require and you do not want to continue the iteration, then in such case instead of waiting for the whole loop to iterate through the whole sequence you can jump out of the loop using the ‘break’ statement.

The break statement has the power to stop the iteration of the loop statement and execute some other codes without disrupting your program. Note the break statement is mainly used to terminate the iteration of the program.

The syntax of break statement is :
 
break

An example is given below. In the example, below we will search for the first number divisible by 5 from a sequence.

Link :Python for loop

 
>>> ls=[34 , 78 , 90 , 18 , 100 , 200 ]
for item in ls:
	if item%5==0:
		print( item , 'Is the first number divisible by 5 in ls' )
		break

	
90 Is the first number divisible by 5 in ls

In the above code ‘item%5==0’ checks if the item is divisible by 5. If any item is divisible by 5 the text inside the print() is printed out and the ‘break’ will terminate the iteration. Hence, after coming across 90 the program terminates. Although there are 100 and 200 in the sequence they are not checked since we want the first number divisible by 5 and stop the iteration.

Note if there are codes after the ‘for loop’ statement the program will still continue.

Here is another example. In this example, we will try to find the first number divisible by a number in a sequence. The divisor and dividend are taken from the different sequence. Consider the code below.

Link :Python while loop

>>> ls=[ 1 , 8 , 27 , 33 , 90 , 28 , 88] #dividend
>>> ls1=[ 3 , 7 , 5] #divisor
>>> for divisor in ls1:
	for dividend in ls:
		if dividend%divisor==0 :
			print( dividend ,'is the first number divisible by', divisor )
			break

		
27 is the first number divisible by 3
28 is the first number divisible by 7
90 is the first number divisible by 5

The first ‘for loop’ iterate through the ‘ls1’ and the second ‘for loop’ iterate through ‘ls1’. In the second ‘for loop’ whenever we find a number divisible by the dividend which is taken from the ls sequence we break the iteration and take new divisor from ls1 in the first ‘for’ loop. This process continues until the first ‘for loop’ iterates through the entire elements in the ls sequence.

Form the above example it is clear that the ‘break’ terminate the iteration of just one ‘for loop’. So if you want to break out of two ‘for loop’ you have two write two ‘break’ statement in the appropriate place.

We cannot use break outside any loop statement
>>> i=90
>>> if i>56:
	print(i, 'is greater than 90')
	break
SyntaxError: 'break' outside loop

Since we have not written ‘break’ inside any loop statement so we get ‘SyntaxError: ‘break’ outside loop’.



Python continue statement

The Python continue statement unlike the break statement does not terminate the iteration but rather it helps to jump to the next iteration.

Simply put, whenever the ‘continue’ statement is found the program jumps to the next iteration. It does not matter whether there is a code after the iteration or not the code is not executed.

Link :Python range() loop

Consider the code below.

>>> for item in range( 2 , 10):
	if item%2!=0:
		print( item,'is Odd' )
		continue
	print( item,' is even' )

	
2  is even
3 is Odd
4  is even
5 is Odd
6  is even
7 is Odd
8  is even
9 is Odd

Now if the ‘item%2!=0’ is true then only the print() under the ‘if’ statement is executed. And note the ‘print()’ outside the ‘if’ statement is not executed because the ‘continue’ statement will force the program to jump to the next iteration.

To see if the ‘continue’ statement really forces the program to jump to the next iteration try running the code above without the ‘continue’ statement. You will notice that ‘print( item,’ is even’ )’ is executed for each iteration whether the ‘item%2!=0’ holds true or not.

We cannot use continue outside loop statement

Like the ‘break’ statement we cannot use continue outside the loop statement, if we try to do so we will get an error.

>>> if 89>1:
	print( 'true')
	continue
SyntaxError: 'continue' not properly in loop



Leave a Reply

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