Control statements are under the roof topic of loops and loops in python are defined as iteratively and repeatedly working on source code. Loop control statements are defined as they are intended to change the path execution of the flow of program for its general sequence and the use of control statements is predominantly done here. At one point in the program, we get some requirements in which we want to end the loop completely and exit. And the situation may also arise such as iterating some of the loop statements before we enter the other loop. These loop statements sometimes also referred as Jump statements (or) Transfer Statements in Python.
Based on the flow of execution of the program the usage of control statements is performed. Generally, Python language supports the control statements which are of three types and they are:
This break statement is used to break the execution of flow of the program based on the user requirements. It applies as it brings the flow outside the loop when the code is concentrated on another source block. And the break is only used in the code after if Condition inside the loop. If it is present in the nested loop the break will terminate the loop. In the whole code whenever the break keywords appear the interpreter comes out of the loop and executes the next immediate statements of the loop.
#Taking string as input for i in ‘Dubai’: if i == ‘b’: break print (‘current letter:’, Dubai)
>>> current letter: D current letter: u
In this output window, we get that string as input INDIA and break statement works as the D in the string encounters with required conditional expression.
NOTE: When we use break in the inner loop, it mainly focuses on breaking out of only inner loop and the outer loop works and execution takes place significantly.
Continue statement works as in the code when continue appears the Python interpreter automatically jumps over the execution of the rest of the code statements in the respective loop and moves on with the next iterative statements. It is also used to end the particular iterative loop in which for or while is present and the continue keyword goes to the next iteration.
for char in ‘Dubai’: if (char == ‘b’) continue print (“Current character:”, char)
Current character: D Current character: u Current character: a Current character: i
In the above program the third iteration is executed as true so continue gets compiled and executes the statements respectively. Here, we can observe that for loop has been skipped and the remaining statements have been printed.
Continue doesn’t work on the principle of quitting out from the loop or not terminating rather it works on the principle of iterating the other next statements. Continue in Python works on the feature that it automates the control to the starting part of the loop. It is used to quit the iteration in which it is executing the loop and continue for the next iteration. Continue statement is not used to outside the loop because it depicts an error which is syntax error. Anyways continue is generally used in all types of loops but significantly used in for loop, while loop, and do-while loop. Continue is one of the reserved keywords in the Python.
Pass is referred as null operation in Python. Pass is used in a situation where a user is not sure about the code and does not want to implement the code and there the pass statement comes into the picture and pass statement is implemented. Pass is used as an alternative for the empty space of the code and the user also avoids having error in it.
A = 5 for i in range(A) ; #code that is to be deployed in future for a in ‘CODE’: if(i == ‘D’) pass print (i)
C O D E
“In this code after the for loop, there is nothing written but the user wants to execute the code so the user just uses pass keyword so that the interpreter skips that part of that code”.
The main role of pass is that it avoids errors that are compiled by an empty loop.