• last month
Transcript
00:00Hello everyone, this is Sandeep. Today I'm going to start loop.
00:07In loop the set of statement carries on till the condition is true and when the
00:15condition become false the loop terminate.
00:21Loop means iteration or repetition. So we apply the condition in such a way
00:28that we will get more than one results and when the condition get false the
00:34loop terminate.
00:38When the condition is true then the set of activities will carry on and when the
00:45condition is false and the loop terminate. So we can understand how the
00:50loop works with the help of this simple program that is display first three odd
00:55numbers as I have to display first three odd numbers 1, 3 and then 5.
01:02So let us understand it. First of all I have taken two variables one for the
01:07odd number that is od, I have taken i to count the odd number. So od I
01:14initialize the value of od that is 1 and this is the first odd number for
01:19the counting of it I have taken a counter variable i equals to 1.
01:24So here it will come 1 less than 3 as I have to display only the first three odd
01:30numbers 1, 2, 3. The loop is going to work for the three times that's why in a
01:35decision box I have written i less than equal to 3. Let us do a dry run what is
01:41value inside i it's 1, 1 less than 3 the condition is true it's going to come
01:47to the yes part and it's going to print what odd. What is inside of od it is 1
01:54so it's going to display what 1. Then I need what 3 so what I will do I
02:01will write a question in such a way od equal to od plus 2. So what inside
02:07od it is 1, 2 is added to it 1 plus 2 now inside od it is what 3. So please
02:15remember the values of the variables okay inside od it is what 3. Now it will
02:21come to the next equation that is i equal to i plus 1. What is inside i it is
02:271, 1 is going to add with it so 1 plus 1. Now the new value inside i it is 2.
02:35The loop will carry on 2 less than 3 because now inside i it is 2. So 2 less
02:42than 3 yes the condition is true it will come to the yes part. What is inside od
02:47it is 3 so what it is going to print 3. Then what 3 plus 2 inside od it's 3, 3
02:56plus 2 that is what 5 and here the equation 2 plus 1 that is what 3. So
03:02inside i it's 3. Again the loop will carry on 3 equal to 3 yes the condition
03:08is true it will come to the yes part. What is inside od it is 5 that's going
03:13to print what 5 okay and 5 plus 2 7 and 3 plus 1 4. Now inside i it's 4 inside
03:23od it's 7. It will come here okay here 4 less than 3 the condition falls it will
03:33come to the no part and then would stop. So we get 1 3 and 5. I hope you
03:41understand how the loop work. I'm going to implement the same program in C in my
03:47next video. Thanks

Recommended