Pyhton Control Statements
Control Statements
There are three control statements that can be used with for and while loops to alter their behaviour.
They are pass, continue and break.
1. pass:
Whenever loops, functions, if statements, classes, etc are created, it is needed that we should write a block of code in it. An empty code inside loop, if statement or function will give an error.
Example:
Output:
To avoid such an error and to continue the code execution, pass statement is used. pass statement acts as a placeholder for future code.
Example:
The above code does not give an error.
2. continue:
This keyword is used in loops to end the current iteration and continue the next iteration of the loop. Sometimes within a loop, we might need to skip a specific iteration. This can be done using the continue keyword.
Example 1:
Output:
Example 2:
Output:
3. break:
The break keyword is used to bring the interpreter out of the loop and into the main body of the program. Whenever the break keyword is used, the loop is terminated and the interpreter starts executing the next series of statements within the main program.
Example 1:
Output:
Example 2:
Output: