My name is Atharva and we are continuing this C programming course. (pause 6 sec)
In the last video, we had discussed loops used in C programming, we had seen what exactly are loops.
Then, we saw why we use loops in programming, what type of problems we would solve using them, then we saw what are the types of loops.
We saw that there are two types of loops.
The first type is the entry control loop, and the second type is the exit control loop.
In this video, we will discuss exit-controlled loops.
In part two, we would discuss exit-controlled loops, which means that the loop will run once without checking any condition.
After that, the condition will be checked, do we have to go inside the loop the second time after checking the condition or not.
These are our exit control loops, and in this, there is only one type of loop, which is called as “do while loops”.
It is similar to while loops, we will quickly see what its definition is.
This says, “Do while loop is similar to while loop, except the fact, that it will execute once even if the condition is false.
Now, this is something to pay attention to, what used to happen in while loop, first the condition used to be written before the wild keyword.
First we used to check the condition and after that it used to depend whether to execute the statements inside the while loop or not.
But in this loop what will happen, in do while loop what will happen is, since it is an exit control loop.
It will not check the condition at the time of the entry, it will enter, it will execute the inside statements.
After that, it will check the condition, whether I have to execute the loop the second time or not.
So, if the condition is even false, at least once this loop will work.
We will also use this loop and see how this works practically, before we will understand its sequence.
So how will its syntax be? Syntax will start with the “do” keyword.
Like in “for loop”, we used to write for, for while loop we used to write while.
In the same way, the starting of the do while loop will be with “do”.
That's how the compiler will know what is the difference between, while loop and do while loop, the difference is that the start happens with do, which means with the do keyword.
And after that our curly brackets come.
And there are statements in curly brackets.
The statements that will come inside the loop will go on executing, after the curly bracket ends, our while keyword comes.
And along with that, in the round bracket condition will also come here.
We will see this in Visual Studio Code how it looks.
But for now, we will understand how it is different from the while loop, before that, ‘do’ is written and after those statements come.
After the statements get over, after coming out of the bracket.
We write the ‘while’ keyword and then the condition.
So, you must be understanding the flow, how at the time of the entry there is no condition, after the loop or the bracket gets over, the condition is given.
So, the exit control loop is somewhat in this way.
Now, we will go ahead and see how its sequence or flow is.
Flow is of this type, first of all, the loop’s body or the statement inside the loop will get executed without checking any condition, then we will go on to step two.
What will be step two? Step two is checking the condition.
So, we will check the condition, if it is true, we will again go to step one and what was step one? To execute the body of the loop and if the condition is not true, we will move on to step three. What is step three saying? Coming out of the loop, the statements that are ahead of the loop in the program, the statements apart from the loop will start executing.
So, the sequence is somewhat of this type, how the ‘do while loop’ works.
Now we will go ahead, and understand them practically…
So, this is our Visual Studio Code, our text editor, and here we have written one simple program about the ‘do while loop’.
How will this, ‘do while loop’ works, we are going to see that.
Basically, what is this program doing? From you, means the user of the program, the output screen the person might be seeing.
From him, it will take a number.
It will take an input number and he will check that number whether it is more than 10 or not.
So first, it will take the number and it will print it, and show you that your number is this.
Like I have put five.
So, what will this ‘do while’ program do? As soon as I write my file, it will also make such a sentence and write, your number is five.
We will see how it is written.
We have entered five and he again tells us that your number is five.
This will go on till the time our number doesn't go above 10, or more than 10.
We will see how it works.
First line if we see, it is our preprocessor command, the second line would be of our main function.
The main function is also proper, and we can even see its return zero below.
So, we know from where the main function starts, and where it ends.
After that we are initialising one variable, which means we are writing its data type, we are defining it So, what is the name of the variable NUM, which we call as num.
So, what is num? It is an integer data type variable.
Now, we have made num.
Now the do while loop will start.
How will the ‘do while loop’ start? by writing do.
I have written DO and my bracket starts from here, these are curly brackets, whatever is inside these curly brackets, could be the body of the loop.
So, we had seen the sequence that first of all the body of the loop gets executed.
So, here as soon as the compiler comes, he will see that do is written, he will quickly come inside and start executing the statements.
So, the first statement is of scanf.
So, we had learnt about printf and scanf functions.
Printf helps in printing the output, and scanf helps in taking the input.
So, scanf basically will take one value from the user, and what will be that value, it will be stored in num So, it will obviously be integer type, that's the reason why we have written the format specifier as percent D, percent D means it is going to take integer value.
So, we will write percent D, and the integer value will go and get stored in a NUM means num variable.
In the num variable, the value has got stored, scanf’s work is over, it took one value input and stored it in num.
Now we will go ahead, and see the printf function.
What is happening in the printf function? A statement is written, your number is and there is a colon given.
And now after this, we will print the number which the user has basically entered.
Again, we have used the format specifier.
After the format specifier one backslash N is written.
Backslash N means, next line.
Whenever you will see backslash N in double quotes, this means that you must understand this at once, that your number is printed in the first line.
After that whenever backslash N will come, our cursor will come on the below line, and the next statement which will be printed, will be in the below line.
Now, we will execute it and run it and see how it is getting printed on the below line.
So, this is your number, we have also mentioned the variable’s name from printf.
What happened in these two lines, we have taken a number from the user and the same number we have displayed that your number is this this.
So, this was very easy, after this we will go ahead and see what our ‘while’ condition is saying.
So, here our loop’s body gets over and the curly bracket comes.
Now we have written the ‘while’ keyword and we have written the condition.
What is the condition? num should be less than 10.
Now, from this you must have understood that, the user will keep on entering the number and we will keep on printing this number, till when? Till the time the user doesn't type the value more than 10.
If he has put five, we will say that your number is five.
If he puts four, we will say that your number is four.
If it puts seven, we will say that your number is seven.
If he puts 10 or more than 10 like, 15, 20, 25, 30, some value of this type.
What will happen at that time? At that time this condition will be checked and what will be happening in this condition? it is not less than 10, whatever has come in the num, is not less than 10.
So, the condition will be false, and again our loop will not be executed.
So, what should be in this particular scenario? If I would have written this condition earlier itself, how I would have taken the number from the user.
So, I would have to take the number from the user at least once on the basis of which, I will check that the number that has come, is it less than 10 or more than 10.
That is the reason why, the ‘do while loop’ comes to work here.
At least once our condition or statement will run or print.
On that basis it will be decided whether the loops coming ahead will be repeated or not.
So, we will see how this program works.
I have right clicked here, and run the code.
So, you can see here, that there is an option of putting the input here.
I will input the number seven, and press enter; you can see that the first time, it has run without checking the condition.
That is the reason why, it has printed your number is seven, because you can see that your number is inside the loop.
So, you must be understanding that, for the first time it will not check the condition.
Second time it will start checking the condition, because this is an exit controlled loop.
It will check the condition at the time of the exit.
So, it checked the condition at the time of the exit.
We got to know that 7 is less than 10, so it has given us an input option again here.
Now, what I'll do here is, I will write 10 here, on 10 the condition should get false.
So, we will check what will happen.
I will enter, it has got printed your number is 10 and after that, it has closed this loop.
So, you can see, how the loop runs the first time, and after that the condition is checked at the end.
So, in this way our do while loop works.
And we can create scenarios and we can do different things and different programmes by making different conditions and statements.
So, we will keep on practising this.
We will go back to our presentation, and the one more thing that we are going to see in this video.
That is the nested loops.
We had discussed in the last-to-last video, the nested if statements, nested switch statements.
With this we know that what is nested, nested means one inside the other, if we're talking about nested loops.
Here for example, I have taken a for loop, what I will do in ‘for loop’ is, after the ‘for loop’ starts and in its body I have written one more for loop.
So, here if you see the syntax.
The syntax says that it is for loop, its initialization, test condition and increment we have written.
And then its curly bracket started.
And inside the curly bracket we have written one more for loop and the other for loop also will be of the same type, it will be similar.
We cannot put exactly the same, if we put it exactly the same, the outside and the inside variables are the same, there will be a problem, the compiler will not get to know which value we have to increase and which value we have to reduce.
We'll see these things in the coming programs as well.
For now, we will understand that the top loop, which means the outer loop and the inside loop will be the same in syntax.
But its values and conditions will be a little different.
So, the other loop will also be of that type.
There will be initialization.
test condition and increment.
Then inside it, there would be some statements, and if we have to write some statements in the outer loop, after the curly brackets get over, we will write a few statements.
In this way our nested loop will be there.
We will also see this with the help of practicals.
How our nested loops are made.
So, we will go into the Visual Studio code, and we will come to our nested loop program.
Even this is a very simple program.
Initially it must be looking very complex but actually it is not, because we have already discussed for loops, that is why it will be easy for us to understand.
We will start with the preprocessor command.
So, we have seen that STDIO dot H is there.
Then comes up our main function.
Main function or the main door has started the program.
We will come inside and see that we have defined two variables.
First is I and second is J, I and J are basically for two loops, which we will initialise and based on that number of times they are going to run will be decided.
So, our first loop is getting started, which is ‘for loop’.
Our ‘for loop’ has started.
Here we know the first thing is initialization, inside ‘I’, I have initialised the value 1, now we will go ahead, after semicolon comes our test condition.
So, what is the test condition?
Test condition is that the value of ‘I’ should be less than 5.
So, we also understand that it started from i1.
And what is the test condition? As long as the I value is less than five, till that time the loop should go on.
And the third thing that we have written, is our increment, how will be our increment, it will be plus one, plus one.
So, we have written double plus over there.
So, this was our outside loop, and in this what is the work that we actually want to do, even that we will understand.
What is our nested loop actually doing? In this particular program? It will print 1 2 3 4 5, 1 2 3 4 5 has easily been printed with counting, through my ‘for loop’ or my ‘Y loop’ and why there is a need for a nested loop, because I have to print this 1 2 3 4 5, 5 times.
So, I have to print 1 2 3 4 5, I have to print this 5 times.
At that time, our nested loop concept comes.
What will happen in this is one loop will run, and it will keep changing our line.
How will it change? Here you can see that backslash N is there, because of this, it will make the next line.
In this way our line will change from the outer loop.
And what will the inside loop do? It will print our counting 1 2 3 4 5, in this way our loop will run, we will see our outer loop.
It will start from one, and it should go till five, means five times.
It should run five times.
Yes, it will run five times.
After that we have seen this printf statement.
What is this printf statement doing? It is changing the line because inside it, backslash N is written.
So, it will directly change the line.
We will come ahead of it.
Ahead there is one more ‘for loop’.
Second ‘for loop’.
That's the reason why we are seeing the next loop, the second for loop in which the variable is used is J, J is also starting from 1 and what is the test condition of J? Whenever the J’s value will be 5 or less than 5.
It will keep on printing the statement inside it, and the for loop will carry on.
Third thing is our increment operator.
It is the same everywhere.
We will come inside, what is happening inside, our J’s value is getting printed.
How do we know that, J was a variable and in the variable percent D has been used, which was format specifier.
In this way, we got to know that we are printing J’s value.
So here there are two loops running.
There is an outside loop that is running and inside that one loop is running.
What is the outside loop deciding? You have to print in the first line after that you have to come to the second line.
And the inside loop is helping in printing, which means it is helping in writing the counting of 1 2 3 4 5.
What will happen here? If you see the outside loop, it is starting from one, if it goes on till less than five.
It will go on for only four times.
It will work when the value of I is 1, when I’s value is 2, when I’s value is 3 and even when the I’s value is 4.
But when the I’s value is 5, then here, the test condition will fail.
As, 5 is not less than 5.
So, it has failed.
So, the loop will not work.
Then we will also have to put an equal to sign here.
So that we can run it five times.
So, we will save it with Ctrl+S.
Now we will right click, and click on the run code.
So, here you can see how our counting has been printed five times.
How did we print it? With two loops, the outside loop was changing lines for us.
After the first line we changed the line, and the second line came.
Then again we changed the line and the 3rd line came.
So, these lines were changed by our outside loop and the inside ‘for loop’ which was there, it was printing counting.
Here you can see that the J’s values are getting printed.
First time J was 1 then the J's value got incremented and the second time J became 2.
So, 1 2 3 4 5, in this way five times J's value changed and as soon as the J’s value became 6 with increment, this test condition failed.
This came out, and after coming out the for’s value had got implemented.
So, in this way it can go five times like this, and five times even like this.
So, because of this, we had to invent a nested loop and we had to use it in programmes.
Here we have written only two loops, one inside the other.
We can write as many loops as we want and then manipulate the values, with the help of these nested loops, we can even design few patterns, like we can make pyramid shape only with the printf statement, or we made any square shape or any rectangle shape, in this way by running different loops, how many number of values should come in this line or like this? All these things we can decide and with the help of nested loops we can make different designs.
In this video, we discussed how the do while loop, which means the exit control loops work.
After that we saw the nested loops, what was the need for the nested loops.
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 queries.
In the coming video, in our next topic we will discuss, break, continue, and go to statements.
In the switch case statement, we had heard of this break keyword name, and we had seen what is the meaning of break, from that particular flow or from that particular switch case statement, it comes.
So, this is the brake’s work.
We will understand it one by one properly in our upcoming video.
We will even see how they work in loops. And how are they used. Till that time, thank you so much.
Very happy 😄😁
Very happy and greatful, thank you , great course.
Super for poor students to learn software course languages TQ