Control statements alter the sequential execution of a program.
Examples
- if-elif-else
- while, for
- break, continue
Break
Break statement makes the program exit a loop early.
Using Break
Generally, break is used to exit a loop when a condition is satisfied.
In the below example, when the variable
Code
Output
Break in Nested Loop
Break in inner loop stops the execution of the inner loop.
Continue
Continue makes the program skip the remaining statements in the current iteration and begin the next iteration.
Using Continue
Generally, continue is used to skip the remaining statements in the current iteration when a condition is satisfied.
In the below example, when the variable
Code
Output
Pass
Pass statement is used as a syntactic placeholder. When it is executed, nothing happens.
Generally used when we have to test the code before writing the complete code.
Empty Loops
We can use pass statements to test code written so far, before writing loop logic.