• last month
Transcript
00:00Hello everyone in my last video I have explained that how loop within loop works in a flow
00:09chart.
00:11So we have understand this program that to display 1 then to 1 2 and 2 and to 3 in flow
00:18chart.
00:19Today I am going to implement this program in C with the help of code blocks.
00:27I am going to make the program in C, let me open the code blocks and inside it I am going
00:34to create a new file.
00:41I will include the header file that is https://stdio.h I will save it also, I will click
00:51on the save file, give it a name to it loop1 and then choose save as type cc++ files and
01:02then save.
01:05I will use int main and inside it I will take two variables one is i another one is j.
01:17I will apply the outer loop that will work for rows.
01:22So i equal to 1, how many rows that is 3 rows, if we see the flow chart also we need 3 rows
01:331 then 2 then 3.
01:37On the basis of that here I have used what i equals to 1, i less than equals to 3 then
01:47i++ and the inner loop I will apply for the columns.
01:56Here I have to think that what logic should I apply so for here j equals to 1 and I compare
02:06inner j with i, j i less than equals to i and then j++, we will do the dry run and see
02:17is it working fine or not.
02:20Once it will come to the inner loop and as I applied I will display the value of j that
02:28is I will use the specifier %e, j.
02:34I need some space also after printing of the value so I will use what slash t for giving
02:47a distance and here after the inner loop outside of the inner loop I will use printf and use
02:58what slash n to take the cursor on the next line.
03:04So this is the program friends of printing 1 then 1, 2 then 1, 2, 3 of loop within loop.
03:13Let us save it first I am going to file and clicking on save file.
03:19First thing what I will do I will click on the build so it's showing me 0 errors, 0 warning.
03:27Now let us run and see is it going to display yes 1 then 1, 2 then 1, 2, 3 so we got the
03:35correct result so how the things are working let us see how the loop within loop works
03:42let us see how it's going to print 1 then 1, 2 and 1, 2, 3 let us see.
03:47So here I have taken two variables i and j this loop is the outer loop which is going
03:54to represent for the rows and this one is the inner loop which is going to represent
04:00for the columns okay.
04:02So the value of i is 1 okay it will come to the descent part 1 less than 3 the condition
04:09is true immediately it will come to the inner part inner loop what is the value of j it's
04:151, 1 less than equals to i that is 1 equal to 1 so it's going to print the value of j
04:23that is what 1 so 1 is going to print and some space is also going to print after printing
04:301 okay so once it will complete the value of j is going to increment so now the value
04:39of j will be what 2 it will come to the decision part 2 less than 1 the condition will get
04:46false once the condition will get false this inner loop will terminate and it will come
04:53to the outer loop once it will come to the outer loop before that we have used the slash
04:59n so the cursor is going to move to the next line or the next row okay so it will take
05:07the cursor on the next row and then the outer loop value of i is going to increment that
05:15is earlier the value of i was 1 now it's going to increment by 1 so now the value will be
05:22what 2 it will come to the decision part 2 less than 3 the condition is true and once
05:29it will come inside the inner loop the way it is going to start the inner loop is going
05:35to start from its initial value that is j equals to 1 so the value of j is 1 it will
05:41come to the decision part 1 less than 2 as the value of i is 2 yes so the next line it's
05:48going to print what 1 okay and the value of j is going to increment okay and now the
05:55value of j is what 2, 2 equals to 2 the condition is true so after some space it's going to
06:03print what 2 so in this way the things will work okay and we'll get the results now the
06:15value of j is going to increment to from 2 to what 3, 3 less than 2 the condition will
06:23get false this loop will terminate the cursor will come to the next row okay the value of
06:30i is going to increment by 1 that is what now the new value of i will be what 3, 3 equals
06:38to 3 the condition is true again it will come to the inner loop now this time the value
06:43of i is 1 it will start from its initial value and what is the value of i it's 3 so 1 less
06:50than 3 the condition is true so first the value of j is going to print that is not 1
06:57okay with some space after that some space and the value of j is going to increment that
07:03is what 2, 2 less than 3 so the 2 is going to print and then after it some space then
07:10the value of j is what 3, 3 equal to 3 so what 3 is going to print and after it some
07:18space then the value of j is what 4, 4 less than 3 the condition will get false the inner
07:24loop will terminate it will come out the cursor will going to come on the next row and it
07:30will go into the outer loop where the value of i is going to increment from 3 to what
07:344, 4 less than 3 so the outer loop will also gets terminate it will get the result so let
07:43us run it and see it's showing the result okay so I hope you understand how the loop
07:51will loop works in my next video I am going to explain something else thanks for today.

Recommended