Hello everyone.
My name is Aaditya and we are continuing with this C programming course in which, we had discussed in our last video loops used in C programming. (7 seconds pause ; music)
We had seen how the loops are used and using them, how we can make our program effective.
We had discussed the answers to this question in our last video, and we saw that the loop is based on a condition, which means based on some condition there are a few sets of statements that are executed repetitively and because of these functionalities, we have to use the loops.
After seeing these loops, we are going to see 2 or 3 more statements in this particular video, which are called ‘break, continue and go to statements’.
These statements are majorly or primarily used in loops and we will see how their functionalities are different from each other.
We have seen break statements earlier in switch case statements, like all statements of one case statements have run.
After that we write one break statement, which means that after this case, no future cases will be executed and they will not be tested.
So, this was the break functionality.
In the same way, we will see the other statements as well that how it works and where their effectiveness comes to work.
First of all, we will quickly discuss the break statement.
What is the break statement according to the definition?
“The break is used in terminating the loop immediately after it is encountered”.
(01/49)
Now, what does this mean? In the Loop wherever you will find break written, B R E A K and semicolon.
Now, what does this break means? This break statement has come in your loop.
And it basically means that, as soon as the compiler will read the statement, it has to come outside of your loop.
Whenever in the loop on the top, middle or at the bottom, whenever the compiler will get this loop statement.
It will exit the loop and come out and start executing the outside statements.
So, this is the functionality of our break statement.
This we will understand with an example.
This is a simple program.
In this, we can see that first of all we have written the preprocessor command.
Then we have written the main function and in the third line we have also defined one variable,
which type? integer type.
So, we have done these three things.
After that our for loop has started.
So, what is this for loop doing?
If you see the first statement, in this we have initialised the ‘i’s’ value by zero.
So, this is our first part.
In our second part there is a condition, what is the condition saying? It is saying that till the time ‘i’s value is less than 10, till that time ‘for loop’ will continue.
So even this is clear.
After that the third part which is there, it is i plus plus part that is the increment part, as and when the entire loop will complete, it will increment the value.
After clearing all these three things.
Now we will come to ‘for loop’.
So, in ‘for loop’ we will see the PRINT F statement which is there, basically what is doing is printing the value inside our variable ‘I’.
Like in the start i’s value is zero, the PRINT F statement will write our i’s value as zero, which means it will get printed on our output screen.
So, this is the 1st part.
Okay !
So, if we come down inside the for loop statement, we can see there is also an ‘if statement’.
What does the condition of if statement says, it says that ‘i’ is, that means the value inside the ‘i’ statement is equal to 5? For now, the i variable value is zero.
And it is not equal to five.
That is the reason why the statements in the if statement cannot be counted.
And our loop will increment and move on to the second condition.
So, what will happen in the second case, i’s value has become 1 as increment has happened.
We will check in one and the PRINT F statement will be printed.
In PRINT F statement earlier it was printed zero and now, it will be printed one and in the same way, our i’s value will be from zero to 1, 2, 3, 4 and even five.
Now the value is five so it will print five.
But the statement below the loop, which means the if statement, in that the condition will be true, which means if inside i , 5 comes, then 5 is equal to 5.
So, this condition is true and it will come inside if.
As soon as it comes inside if, what will happen is you will get a break statement, which means the compiler will get a break statement written.
What does it mean?
That whatever loop is going on, break it and come out of that loop.
As soon as this break statement will come with the help of that break statement, the compiler will come outside of that loop.
Since the ‘for loop’ could have gone from zero till nine, which means that our loop could have run for 10 times but as soon as five values have come, our if statement has become true and with the help of break statement our compiler has come out of the ‘for loop’.
So, what does this mean? It wanted to go from zero till nine, the for loop.
But with the help of a break statement, we terminated it till five, which means that it ran for five times.
And the PRINT F statement also ran five times. So, it printed only 0, 1, 2, 3, 4, 5.
(05/59)
It couldn't print ahead as there was a break.
So, the break statement got printed and then we will come out and outside there is PRINT F statement which says that “came outside of the loop’’ and it will tell the i’s value, what was the i’s value at that time.
I’s value was five.
So, in this way our program will work whenever we will put that break statement.
And it will come out of the loop whenever our break statement is encountered by our compiler.
So, this was our break statement.
clear up till now !
We had also read about this in switch and even that works in similar functionality.
Whenever break comes, it leaves all these below cases and comes out of the switch statement.
So, this was our break statement.
Similar to this is our ‘continue statement’.
What does it do?
“It is used to skip some statements inside the loop and continue statements are used”.
So, what is ‘continue statement’ ? It is the continue keyword and semicolon, the statement is only this much and what is its functionality.
There is some part of the loop which we have to skip, based on one condition.
Like if I want to print 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
what will I do? I don't want to print 5.
What is getting printed with the help of PRINT F.
As and when the value is increasing inside the loop.
As soon as the value five will become.
What will I do? I will true the if statement.
I will write continue, like we had written break in the previous one, similarly I will write continue.
What will happen with continue? It will skip the printf statement and will go on to the next step and in the next step the value will be 6.
In the same way, 5 value will not be printed.
Now, we will understand this with another example.
What are we doing in this example?
We have taken a counter variable, which is starting from 10, after starting from 10, it will slowly become 9, 8, in the same way, it will start coming down.
We will see that based on the below conditions.
Now the while loop is starting over here.
What is the condition in while loop? Till the time the counter variable’s value is not more than zero or zero.
Till that time our while loop should continue.
So even this condition is clear.
Now we will come inside the while loop and we will check from the start when our counter’s value is 10.
So, we will check is 10 equal to seven because the if statement is asking that, is the counter statement value equal to 7? We will say no, it is not.
So, that is the reason why the counter minus, minus is written in if and continue is written, that will not be executed.
It will not be executed till the time its value is 10, 9, 8, till that time, it will not get executed.
This if part, these three lines will get skipped and below it will start printing inside the counter whatever is the value, like we started from 10.
So, 10 has been printed.
We'll come below and see that counter minus minus is happening, which means that decrement is happening.
So, from 10 it will become nine and again in nine the condition is true but if one will become false.
So, again these three these three lines will be again skipped.
So, in the same way, as soon as it will be 10, 9, 8, it will keep skipping.
As soon as the counter value becomes 7, this if condition will become true and our compiler will come inside this.
It will come inside and do what? It will reduce the value of the counter.
(09/28)
It will make it six from seven but it will continue.
What will this continue statement do, this PRINT F statement which was there below.
And this counter minus, minus statement.
It will skip all of them and again will go up in the loop to check the condition.
So, what we do with the help of ‘continue statement’, we skipped the below two statements and we went again went up in the condition, to check if now the value is getting true or not.
oSo this works in a few scenarios, where we have to skip some loop’s part based on some condition.
So, here the entire game is working based on the condition.
There are conditions in while and in if as well.
In the last case as well the outside loop condition which was there, it was working on that basis also.
As well as the ‘if condition’ in the loop, it was working even on that.
So, here we saw how the continue statement works.
Before that how the break statement works, even that we understood.
Now, we will come on to the third statement, which is called the ‘go to statement’.
What is the ‘go to statement’?
“go to statement helps jump compiler to a particular label in the same program”.
Now, this is basically divided into two parts.
One is its label part and the second is the ‘go to statement’ part.
Now, we will see how to understand this.
Here on the right side, you will see the example where it is written go to label.
As soon as our compiler comes up with this statement,
what will it do? The label name keyword, wherever he can see it.
It will go there and start executing the statements after that.
So, here it might look complex, we will understand it with a basic example and we will understand it very easily.
We have imported a few preprocessor commands.
After that we have started the main function, in the main function we have declared a value inside the variable which is 1.
In I there is 1 value saved.
Now, we will come to the label, this is basically label.
We can give any name to this label.
Here we have given count.
We can keep it count two, whichever is our identifier’s name.
Basically, we can give any of those names to this label.
So, we have given here a name count and before the count we will put a colon.
So, it means that this is our label and the further statements will get executed in the same way, till the time we don't get the ‘go to statement’.
So, we will see what is happening.
We came here, we defined the variable, we saw that the label is given, we will go ahead, the compiler will see that there is PRINT F statement.
In PRINT F we have to print i’s value.
So, the i’s value is 1.
So, the i’s value will be printed as 1.
Then i is plus plus means the i’s value will again become 2, then will come if condition, in if condition what is it, 2 is less than 10…
Since 2 is less than 10, we will come inside if condition.
What is written in if? Go to count.
So, go to is a keyword and count is our identifier basically and is one label.
So, whenever you get this statement, which is saying go to count, it means that you go to the count.
(12/59)
So, as soon as our compiler encounters this, what will it do?
Wherever the count label is written, it will again go there and it will start executing the statements again.
So, the statement is PRINT F, so it will execute the PRINT F statement again.
Then, i plus plus was executed.
So, in the same way whenever our compiler will encounter this goto count statement.
It will go back to that particular label and from there it will start executing the statements from there again.
So, these were our three statements, first was ‘break’, second was ‘continue statement’ and third was ‘go to statement’.
So, these three have different functionalities.
How they have to be used in loops.
How the conditions have to be manipulated.
All these things, we can do with the help of these statements.
In the coming programmes, we will use them and understand its functionality properly.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
In the coming video, we will be discussing functions used in C programming.
We will see, how to work with functions and basically what are these functions so we will discuss all these questions in the coming video.
Till that time.
Thank you so much.
Thank you everyone.
Share a personalized message with your friends.