In this article, you will learn about the Loop control statement, the necessity of using these statements to control the loop's flow, and their use according to our requirements.
The Article has been organized in the following manner:
Break and Continue statements are also known as Loop Control Statements, as they automate the working of the inner loop. Loop automates the task and repeats in a certain manner, but there are some special cases where we need to terminate the loop or skip some iteration. There we use a Loop Control Statement that is Break and Continue.
It works similar to a break statement in C. It terminates the current working loop and passes the control to the next statement, and if the break statement resides inside the nested loop, it passes control to the outer loop. It can be used with both while and for loops.
Labelled break statement: It terminates the loop's outer statement, which has been marked with the given label.
Unlabelled break statement: It terminates the innermost statement of the loop
Flow diagram of break statement:
Here are the steps involved in the flow chart given above explaining a break statement:
Let's consider the condition where we want to iterate a loop from 1 to 10. When 6 is encountered, we want to break the loop and return to the outer condition. Here we use break statement with for loop and if statement.
#Demonstration of Python break statement #using for loop #loop from 0 to 9 for i in range(10): # if i is equal to 6 # break statement terminate the current working loop if i == 6: break else: print (i)
0 1 2 3 4 5
It is the second form of the Loop Control Statement, very similar to the break statement. But when it comes to the working of the continue statement, it works just opposite to the break statement. Instead of terminating certain conditions, it jumps off to the very next condition. But it will continue the execution of the loop statement as per its name.
Forms of continue statement:
Labelled continue statement: It skips the iteration of the outermost loop, marked with the label.
Unlabelled continue statement: It skips the iteration of the innermost loop.
Syntax : continue
Flow diagram of continue statement:
Here are the steps involved in the flow chart given above explaining a continue statement:
The continue statement is used in the program to skip any iteration, such as when we don't want to print a certain statement. Consider the example of a break statement altering the condition. There are numbers from 1 to 10, but we don't want 2 to come and we don't want to increase the number of looping statements in our program either. For this we should use a continue statement.
Refer to the below implementation:
#Demonstration of Python continue statement #using for loop #loop from 0 to 9 for i in range(10): # if i is equal to 2 # continue statement will skip #and start executing next iteration if i == 2: continue else: print (i)
0 1 3 4 5 6 7 8 9
Pass statement is not found in traditional C language; it works like a null operation because nothing happens when the pass statement is executed. It is used in Python when any statement is required syntactically, but we don't want any command or code lines to be executed.
Uses of Pass Statement:
#Demonstration of Python pass statement #using for loop #loop from 0 to 9 for i in range(10): # if i is equal to 6 # pass statement will execute #but there will be no impact #on the looping statement if i == 6: pass print ‘Just a pass statement’ else: print (i)
0 1 2 3 4 5 Just a pass statement 7 8 9
These are the loop control statements, which help us to terminate or skip some required statement under the normal procedure. Every statement has its function, and none of them can be considered to be better than the other, as they each have different aspects and usage.
Your email address will not be published. Required fields are marked *