Python loop control statements- Break, Continue, Pass!
Python loop control statements- Break, Continue, Pass
Hey there! I hope you're doing great!.
Today let us check what are loop control statements and how they work?
Break, Continue are the statements used in loops. Loops are used to execute a block of statements repeatedly, there may occur some conditions like we need to stop the iterations if an particular condition occurs if the condition won't occurs then complete loop should run or there may be a condition where we need to skip an iteration.
Python supports 3 control statements:
- Break statement
- Continue statement
- Pass Statement
The Break Statement:
In python the break statement is used to terminate the current loop. It will exit the loop when break is executed. If there are any statements after break statement it will execute them by exiting the current loop, it will come out of the loop unless it is nested loop. In case of nested loop it will exit only the loop which has break. Lets see with examples to get more clarity on it.
Here is an example to show how break works
This is a program to print first 5 even numbers of a given number. Value of n is 15 so the possible even numbers are 2,4,6,8,10,12,14 total 7 even numbers but we need to print first 5 even numbers this is one place where we need to use break statement. As we can see in the code, in line number 5 there is an if statement to check the count value when the condition becomes true it enters into line 6 where break statement will execute and the control comes out of the loop here for loop will be terminated.
This was the output of the program here we can observe it printed only 5 even numbers. This is how break works
The Continue statement:
In python the continue statement will skip all the statements below it in the current iteration of the loop and it will return the control to the beginning of the loop and it continues from next iteration. It will not terminate the current loop like Break statement. Let us discuss with an example
Example:
A program to print all the number by skipping the numbers which are divisible by 3. To skip the numbers which are divisible by 3 we have used continue statement so when ever the continue statement is executed it will skip the statements in the loop that are present below the continue statement.
Here we can check the output of the program. When ever the continue statement is executed it will skip all the statements below it in the loop.
The Pass statement:
The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. Python won't allow to have empty functions. So we can use pass statement to create empty function where we can fill code eventually. When pass statement executes nothing will happen. Pass statement is a Null operation.
Here is an example:
That's all about loop control statements, hope you found this helpful.
Sasi Kumar M :) 🐇
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment