Hello everyone, my name is Aaditya and we are continuing with the C programming course.(7 seconds pause ; music)
In the last video, we had discussed decision making statements which were if, if else, switch, nested if and nested switch.
Now we are moving towards the loops, which means what are loops and we are going to discuss loops used in C programming.
So, what are these loops? What is the need of them, when we had decision making statements, what exactly is loop, why was it invented and what is the need for it? We will see the answers to all these questions in this video.
So, we will move ahead quickly.
So, if we try to understand it in layman terms.
We will take one example.
Suppose, we have made a circle on the floor and we stood on one point on the circumference and we thought that this could be our starting point.
So, we started to walk from there and walked across the entire circumference because it is a circle.
We came back to the starting point.
So, what happened with it, from where we started, you roamed around and came back to that point, this is called the loop.
And we use it a lot in programming.
Why do we use it? We use it based on the condition and what is the logic that is actually used in is, there is one condition given and we prove that condition to be true which means, if that condition gets satisfied then, we turn around once in the loop and then we check that condition again, if that is satisfied again then, we move around in the loop twice.
So, in this way the repetition of moving around in the loop keeps on happening till the time any particular condition which has been given that has been satisfied.
As soon as it is not satisfied, we will stop roaming in the loop.
Now what is moving in the loop, we will see all these things.
First of all, we will understand the exact definition of the loop.
So, what is the loop?
(02/24)
‘A Loop statement allows us to execute a statement or a group of statements multiple times based on a condition.’
What is it trying to say, based on one condition, any one statement or group of statements, if we're able to execute, it is called a loop in the programming.
We will understand it with an example.
Suppose, I have to print 1 to 10 counting on my output screen.
What I will do in this is, I will simply write the printf statement 10 times in which, 1, 2, 3, 4 the PRINT F statements which we use, I will write that and execute it.
So, what will happen is, my 1 to 10 counting will be printed on my output screen.
I thought that if I want to print the counting from 1 to 100, then what will I do?
This is a difficult task, but what I will do is I will again copy paste the PRINT F statement and I will even write it 100 times.
Because of this the lines of code are increasing but since I have to write the counting from 1 to 100, I have done printf and written it, 100 times.
What happened with this is my program size has increased and I have to execute all the pintf one after the other.
So, in this way a lot of time went into it and the size of the program also increased which is not an efficient way.
So, to do this thing if from 1 to 100 has to be printed then also it is but, from 1 to 10000 I have to print any counting or any such type of series I have to print which should be big in number, will I be able to print it only with PRINT F?
It will be impossible for me.
So, that is the reason why we invented loops.
So, how are the loops used in this.
What will happen in loops is, only once the printf statement has to be written and we will check the condition continuously.
What is the condition that we will check is that I have to print the counting from 1 to 10.
So, what I'll do is, I'll take one variable and store 1 in it.
Okay.
I have stored 1 in the variable.
And the PRINT F statement got printed once and in my output statement 1 got printed coming out of the variable, what happened now is, I increased my variable value.
I increased it to 2, 2 has again become its value and it again came into printf statement.
So, the variable was printed.
So now the value is 2.
So, now instead of 1, 2 will get printed.
In the same way I made the variable value as 3.
So, 3 got printed, I made it 4.
In the same way, I have printed 1 to 10 counting.
I have used only one printf statement, we'll see how the loop's syntax is being used.
I just did this much thing and my 1 to 10 counting got over.
I can do the same thing from 1 to 1000.
I just have to change the variable that many times.
If I have to do 1 to 1000, 1000 times my variable will change.
I don't have to do it manually; my loop will do this entire thing and give it to me.
I just have to execute it once.
We will understand it and see how our loops are going on and making our program extremely easy.
Before that, we will see that these loops will be of how many types.
Like we discussed before running any loop we would be seeing a condition.
Based on this condition, we have divided the loops.
Right ! Okay !
So, based on this condition, we divided the loops in two ways, first is entry control loop and second is exit control loop.
Now, what does this means, entry control loop means a condition is checked before executing the loop, it is also called as the pre checking loop and what has happened in this is our condition which was there, is ‘a equal equal to 1’.
So, the particular condition which is there, that we will take before the working of the loop and after that run the loop.
As I said I want A’s value less than 1 and until that time you keep on running the loop.
So, what will happen in that? I have taken A’s value as 1 which is less than 10.
So, what will happen is it will be executed once.
So, in this way, it is 1st checking the
condition and based on that it is going inside the loop and running the loop’s
statements.
In the same way, the second one is called the exit controlled loop.
Now what does this mean?
“It is a condition where a condition is checked after executing the loop and it is also called a post checking loop”.
It is similar but what is there in it is that 1st the loop will start on its own, which means you will not have to check the condition the first time, when the compiler is
coming out of the loop, at that time it will check the condition to go into the
loop again.
So, this is the thing of difference.
First, we checked the condition then we went inside the loop and ran the statement and then came again the next time to check the condition,
And what happened in the other exit control. First, we went inside the loop, executed the statement, then again checked the condition whether I have to go into the loop or not.
So, in this way the types that we made of the loops, entry and exit.
We will understand these types.
And first of all we will move on to the entry control loop.
And let’s understand that in this entry controlled loop, which types of loops actually come.
The basic characterisation is entry control or exit control and in the entry control loop there are two types of loops with us, which do not need to be made very complex.
For now, we can understand that we can see it here, for loop and while loop.
So, understand that these two types of loops are in entry control and you will understand how they work.
Okay.
Now, we are seeing an entry control loop.
What is there in entry control? First the condition will be checked and after that our compiler will go inside the loop and execute it.
So, the first loop that comes is called for loop,
“It is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.”
(09/00)
Now what can we understand with this, we can understand that it will check the condition and that loop will run for a specific number of time in a repetitive structure.
How? Like we write PRINT F statements and I am running PRINT F statements repetitively as I have to print 1 to 10 counting.
So, now that I have to print 1 to 10 counting, I know how many times I will have to run the loop.
Yes, I know one to 10, which means I will have to run the loop 10 times, which means I have to run the print statement 10 times.
So, in ‘for loop’ we know the specific number of times the loop will run.
So, this is very efficient as we already know how many times the loop will run.
We will not run it more than or less than that.
So, this is ‘for loop’, after that comes the while loop,
What is there in the ‘while loop?’
“It repeatedly executes a target statement as long as the given condition is true.”
In this, we don't know early on how many times the loop or the statement will run.
As long as the condition is coming true, it will keep on running and running and running.
What was happening in the ‘for loop’, we had defined the condition in such a way that it should know that if you have to start from here, you have to stop here.
In the same way what is there in the while loop? There is only one condition on which it will go on running, till the time that condition doesn't get false.
Once it gets false, the while loop will stop.
It doesn't affect how many times it runs.
How many times our loop ran, that is not understood in the while loop.
In ‘for loop’ we understand that early on.
So, we will first start understanding ‘for loop’ first of all, once we get to know ‘for loop’ after that the other loops like the while loop, those would be easy for us.
That is the reason we have to understand it carefully.
So, we will read it.
“So, it is used efficiently to write a loop that needs to execute a specific number of times”.
This we had read early also, so as this specific number of times is continuously brought to mind so that we always remember that, in the ‘for loop’ we know how many times the loop runs.
So, now we will see its syntax, like we were writing if in if statement, switch in switch statement.
In the same way, we would start ‘for loop’ with for.
Which means, we will start it by writing FOR.
We have written FOR, you can see in the syntax here.
After that, we have put round brackets, in round brackets there are three partitions and these partitions will be made with a semicolon, in the first partition there is initialization, in the second one there is test condition and in the third one there is inclement.
We'll understand each one of them.
What is initialization? Assigning anything at the start, in this what we will assign? Till now we have seen that there is a value assigned in the variable.
In the same way, there will be a variable in which we will assign some value which will be our starting point of the loop.
Okay.
So, initialization means storing some value inside the variable from where the loop is starting.
After that second comes the test condition.
So, what is this test condition?
This test condition is that which will tell us, how long will our loop run, for how much time.
What we have done is, we have defined 1 in ‘a’.
A was a variable, in which we have stored value 1.
And what we wrote in test condition? A should be less than 10.
So, as long as a is less than 10, our loop will run.
So, this is our condition.
So, we get to know that ‘a’s’ value is 1 and it should run less than 10.
Which means till the time a’s value is 9, it will run.
Which means it will run 9 times.
So, that's why we are saying that specific number of times we get to know how long the loop will run.
So, these are the first two parts, initialization and test condition.
The third comes from increment.
What is the meaning of increment? We had discussed that we will make the variable and keep on increasing its value so that we don't have to write the PRINT F statement frequently.
So, here we have made the variable ‘a is equal to one’ and what is the test condition that we have given? Till the time the value of ‘a is less than 10’, our loop should run.
In the third part, the third part of the for loop, increment one, in that we will increment our variables value.
So, a's value was one.
We will make ‘a plus 1’ here.
So, what will happen ‘a plus 1’ means 1 plus 1, ‘A’s’ value will become 2.
In 2 it will check the test condition.
In this way, our for loop will keep running till time our test condition doesn’t become false.
So, this is a syntax for now, as soon as we will use it in our visual studio code.
It will be clear to you.
Okay guys !
(14/28)
All the three things in the round bracket, after understanding them we will come in the curly bracket and the body of the loop, the statements that we have written in the loop, that will keep on executing till the time we have to run the loop.
So, this is about ‘for loop’.
We will go ahead and see once what will be its sequence.
The 1st step in ‘for loop’
will be to execute initialization statements.
So, we saw that in the first loop, the first round bracket, we had made 3 parts of it.
First part was of initialization.
So obviously the initialization will be first which means the value inside the variable, which is going to be assigned that will happen the first time, that will be the first step, then what will be the next step.
The second step will be to check the condition, which already comes in ‘for loop’s second round bracket.
So, it will check the condition.
After checking the condition if it comes right, which means it comes out to be true.
So, we will go on to the next step.
If the condition doesn’t come out to be true.
We will directly go on to the last step and what is the last step telling us, ‘we should go out of the loop’.
We will not go in the loop if that condition is not true.
We will come directly out of the loop.
So, here we are talking about ‘for loops’.
So, first of all initialization will happen, after that the condition will be checked and the third thing is to “execute the loop and go to step two”.
What will happen in this, the third step which was there when the condition has become true, you have come inside the loop, all the statements of the loop will get executed and we will go on to the step two where it will check the condition again.
So, in this way, the for loop sequence will be followed.
Step one, step two and step three will come.
If step two is false, step four will come and go ahead.
So, this will be the sequence of the for loop.
We will go ahead; this is our flowchart.
We have seen the initialization, the value got assigned to the variable.
Then the condition is checked.
After checking the condition, if it is true then we will enter the body of the loop and start executing the statements inside the loop.
As soon as the statements get over after execution.
This arrow you can see, it will again go to check the condition.
But if the condition becomes false, we can see in the left arrow.
First, the condition has become false.
So, we straight away go out of the loop.
You can see that “if the condition is true, it will go to the body of the loop, if the condition is false, we will go out on the loop.”
So, this flowchart of the loop structure will always be followed in the ‘for loop’.
With this theoretical knowledge we will move on to our text editor and understand how this for loop works.
We have made this program.
What is happening inside this program?
We have to print 1 to 10 counting.
So, we have seen the first command is pre processor command as always.
The second thing is our main function.
It is also the same.
After that comes our variable.
This variable is integer type and it's called i.
So, we have also made the variable.
Now the main part that comes here is the ‘for loop’.
So, you can see here that we have to do the counting from print 1 to 10.
And here 10 times the PRINT F statement is not written, so that you have to print 1 to 10, 10 times.
You can just see here three line code, this for loop which you can see.
This is a three ling thing and with the help of it we will be able to print our counting.
So, we will see how.
We had seen the syntax.
The first part of the for loop would be initialization.
That is why our variables were i, we made i equal to one, which means we have stored 1 in i.
We have initialised it.
Second part is the condition.
In the second part we are saying i should be less than 11.
Till the time it is less than 11, you keep on running the loop, we don’t have any problem.
After that what is this third part saying? This is basically increment.
So, don’t write it like this, we will write it ‘i plus 1’ because we had seen these operators, this double plus, that you are seeing, these are increment operators.
And they will mean that we have to do ‘i plus 1’.
For now to make it easy, we will write it this way.
So, we have written ‘i plus 1’, so this became our increment.
So, the first part was initialization.
We have initialised the variable.
Second part was the test condition.
We have also written that the test condition should be less than 11.
Third part was increment.
We have also done the increment that should be ‘i plus 1’.
(19/57)
clear up till now.
Now, we will see how it works.
First of all our compiler will come and read the name, ‘for loop’.
Okay.
I will need these three things.
First thing is, I is equal to one.
So, the compiler will say that I have assigned the value 1 in the variable.
It will go on next and say that this is the test condition and the i’s value is 1, it is as it is less than 11.
This condition is also true for the 1st time.
After the condition is true the 1st time, this will be ‘i plus 1’.
How will it be ‘i plus 1’? After executing the entire program, it will be ‘i plus 1’, so this is increment.
Now we will come in this, as our condition was true.
What is this inside it? There is a printf statement, there is a format specifier and there is a variable i.
So, what is happening here? i is our variable and with the help of format specifier the value inside the variable will get printed.
So, we have executed it once when our i’s value is one.
It was one and we printed it.
Now it has again come in the loop.
It will ignore the 1st step.
When it comes in the loop the 1st time, it considers
the 1st step.
After that it ignores the 1st step at the 1st time.
Second time it will directly go to the condition.
Since we have made ‘i plus 1’.
Now the i’s value is 2 but 2 is also less than 11.
So, the condition is true.
Then we will come inside it and then it will be printed.
And this time the i’s value will be printed 2.
Since we had done the increment now, the i’s value has become 3.
In this way slowly the i’s value will keep increasing.
Till when the condition is true, till the time the i’s value is not 10.
If it is 11, 11 is not less than 11.
11 is equal to 11.
So, that condition will not be true and the ‘for loop’ will not continue and we will directly come outside of ‘for loop’, which means we will come here.
And there is return zero which is the end of our program, it is the main function’s end.
So, we will start the for loop from here and end it here.
In this way the for loop we go on.
We will save it by pressing Ctrl plus S and we will quickly run it and see how our output comes.
So it is getting executed.
We have some problems with it.
What we will do here is we will write ‘i plus plus’ and we will save it.
I will tell you why I wrote this plus plus.
So, first of all we will close it.
This has started running infinite times.
So, we have closed it and we will run it again.
So, the 12345678910 counting has got printed with us.
Okay.
Now I will tell you the story of plus plus, what is plus plus basically, it is the increment operator.
Wherever you will put it, like you have put it after it.
It will use the i’s value as it is and it will plus plus the i’s value.
Which means it will increase by 1.
And if this plus plus we put before i, it would have made the i’s value plus 1 and then used it.
understood !
So, in this way the increment operator works.
And we will see, how it is helping our loop to increment the variable.
So, you can see that our 1 to 10 counting has been completed and our loop is running successfully.
So, this was ‘for loop’ and you always use the increment operator here.
You can change the condition as for your wish, you can change the values of the variable.
But always use the increment operator, so that our PRINT F statement should not run for infinite time.
It should run only how much we wanted it to.
So, this was the for loop.
Now we are seeing a while loop,
What is a while loop?
“It repeatedly executes the target statement as long as the given condition is true”.
It is similar to a for loop.
It does not have the for loop’s long syntax.
You just have to write while and have to give conditions in the bracket, that’s it.
Rest, in the body of the loop you can put any statement.
‘While’ is easier than ‘for loop’.
And we will see how it is used.
Its sequence and structure will also be similar.
First the condition will be tested.
If it is true we will move on to step two.
If it is false we will come to step three.
What will happen after coming in step two is that, the body of the loop will be executed and we will come again in step one.
So, we have seen this in for loop as well.
It has the same sequence.
There is a slight difference in syntax, we will see that.
You can see this flow of the ‘while loop’.
It is exactly similar to a ‘for loop’.
The while’s condition will be checked, if it's true it will go into the body of the loop and if it is false, we will come out of the loop.
We will see even this with the help of a program.
Here is our ‘while’ program.
I will close the output window from below.
With the help of ‘while loop’ we are printing 1 to 5 counting.
We can use it in any way.
But for now we are using 1 to 5 counting so that we can print it.
We have seen the include statement, we have seen main, we have seen variable initialization, from here while is starting.
In while, you can see that only one condition is written.
We have first written the variable on the top that i’s value will be 1 and we have written the condition that 1 should be less than equal to 5.
So, if the condition is true the inside statement will be executed.
What is the inside statement? It is printing whatever is the value inside the variable.
This is a simple thing.
After that we will see that ‘plus plus i’ increment operator, that has been put.
So, we have seen it in the last for loop as well.
Where was it? We had written it inside the bracket.
We can write it in the body of the loop or at the end of it.
So, you can see that this ‘plus plus i’, why is ‘plus plus’ written 1st because we have to change the value of i immediately.
When the i’s value was 1, you checked the condition, since it was less than 1, so we came inside.
So, when we did printf, i came as 1 and 1 got printed.
Now we have put ‘plus plus i’, since ‘plus plus’ is before i, it will not remain as 1, it will be 2.
As soon as we go up to check the condition, i’s value would become 2.
But here if after i we would have put ‘plus plus’, it would once again keep the i’s value as 1.
And the second time it would become 2.
In this way, the increment operator’s work.
while syntax is simple, we don’t have to write things here and there, together in a complex way.
So, it is easier to write compared to ‘for loop’.
Now we will even run this and see how it is printing our output.
Here, we can see that simply, 12345, this counting got printed.
I’s value became 1, then 2, then 3, then 4 and then 5.
And even in 5 the condition was true but as soon as the i’s value became 6, with the help of which the condition was not true so we came out of the while and came to return zero and finished our main function.
So, this was a story of for loop and while loop.
We understood both the loops and saw how their syntax is different and how they are used.
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 next video, we would be seeing part 2 of this loop video.
This was part one in which we saw entry controlled loops, the part 2 or the next video related to our loops in which we will see the exit controlled loops, meaning once the entire loop is complete after that the condition will be checked.
Till that time, we will keep running in and we will meet in the coming videos.
Till that time, thank you so much.
Share a personalized message with your friends.