0
How can I improve in The loops?
I am struggling with loops, mainly with the nested ones
4 Réponses
+ 2
If you can link your code and specify what you really don't understand about loops, it will help us in giving a better explanation
Considering there are two loops
for(i=0;i<5;i++){
for(i=0;I<5;i++){
}
}
At first, outer Loop is run ,now the inner Loop is run 5 times or executed fully then the control goes back to the outer loop again ,and it runs second time ,now again inner Loop is executed fully,i.e 5 times ,and so on till the outer loop value reaches 5 means it has run 5 times now ,and the inner Loop has run 5*5=25 times
0
You could try it out an print the values for example and maybe this helps you to see what is happening.
0
#include <stdio.h>
void main()
{
int i,j,rows;
printf("Input number of rows : ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
}
this code prints:
*
**
***
****
*****
******
*******
********
*********
**********
0
First u have to understood the flow of loop . Do dry run then code in editor