Hello guys, welcome to LearnVern. In the last topic we saw Looping statement.
Today we are going to see the Looping Control Statement.
How to control our Loops.
Suppose your loop is working perfectly fine and you want to stop it at a particular point or you want to skip a point or keep it continued at a point...you can control it by using Control Statement.
There are 3 types of Looping Control Statement - Break Statement, Continue Statement and Pass Statement.
Break, Continue & Pass Statement.
First of all we will see what the Break Statement does.
You can see in the diagram…
Your Loop enters, condition comes true, it will break & it will exit the Loop.
But if the condition is true and it doesn’t break, then it will work under Body Loop otherwise it won’t.
Suppose your loop is working & you wrote Break in that Loop after the condition comes out true, it will exit directly otherwise it will start working on the second Loop.
Clear?
Second is Continuous Statement.
What does Continuous Statement do?
If your condition is true, it’ll go under Continue, it’ll skip that particular loop and move onto the Loop Condition.
It skips the remainder of the body. You can control that using the Continue Statement.
Pass Statement works in the same way. What does Pass Statement mean?
The pass Statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.
Meaning suppose you created a function here or wrote a condition and you don’t have to use it but you need it so you wrote it and it is correct syntax wise as well.
But you don’t know what you have to write in its body.
You will write Pass. It will pass.
So this is how the Pass statement works.
Let’s see practically.
I’ll show you practically as well.
I opened Jupyter here.
First of all, I’ll write down the Break Statement here.
We will see how the Break Statement works.
Suppose I have a Loop under which I’ll take a variable named Letter.
And I will roam it under the string named Python.
I put a condition here “if Python’s H comes under Letter”
You have to break it. You have to break the condition.
Meaning the loop should exit there.
I’ll write a comment here “here I’m using Break statement to break the execution”
At last I’ll print the current letters. I’ll write down current letters.
Under this I want to print my letter.
Now what will happen? As soon as it will get H in Python, it will automatically break the loop.
Let’s see. I’ll write Bye here so that we know that our Loop is completed.
Let’s run it.
You can see that the loop was on till H did not come up.
As soon as H came, it gave Bye and came out of the loop and the statement did break.
So this is how you can use the Break statement if you want to break the Loop at a specific point.
Suppose I wrote O here. As soon as O comes, it will break.
Did you see it printed H too this time but it broke the loop before O.
So this is how the Break statement works in Python.
What if I want to work with a Continue statement.
Continue Statement.
What does Continue Statement do?
I’ll explain it to you by giving you an example so that you understand it in a proper way.
I’ll take a For Loop here. We will continue with the letter one.
In Python.
I gave an If condition if letter == H, as soon as H comes, it has to continue.
Now what does the Continue statement do?
When the H comes, it will skip it.
Continue statement will skip the H.
Now I’ll print the current letters.
And at last I’ll print bye.
Now I’ll run it.
As you can see that it didn’t print the whole word Python.
It skipped the H and continued till N.
Meaning it did rotate the Loop regarding strings and characters.
As soon as H came, it skipped the character.
And then continued the loop.
So this is how a continuous statement works.
Our last statement is the Pass statement.
Let’s work with the Pass statement. Let’s see how it works in Python.
For letter in Python. I’ve continued the example.
If letter == H, then we will Pass.
Why did we pass it? Right now I don’t have any logic in mind so I selected Pass.
So that our execution doesn’t stop due to errors.
I’ll write here as well to make you know if our pass block got executed or not.
Now I’ll print the current letters.
And I’ll get Bye printed here to let us know that our execution got over.
Now I'll run it.
You can see the Pass Block written in the middle.
As soon as H came, it printed the Pass Block and later continued.
And eventually brought us out of the loop.
So guys this is how all the 3 statements work.
In the next topic we will work with String, how does String work in Python and what are the other ways to work with String.
So let’s meet in the next video.
Share a personalized message with your friends.