I think the best way is to step through the code manually and write down the line number of the executing code and how the variables change.
Like
L01: int k,i,j,s; // k=?, i=?, j=?, s=?
L02: scanf("%d", &k); // k=18, i=?, j=?, s=?
L03: for(i=1; // k=18, i=1, j=?, s=?
L03: i<=k; // TRUE
L04: s=0; // k=18, i=1, j=?, s=0
L05: for(j=1; // k=18, i=1, j=1, s=0
L05: j<i; // FALSE
L03: i++) // k=18, i=2, j=1, s=0
L03: i<=k; // TRUE
L04: s=0; // k=18, i=2, j=1, s=0
L05: for(j=1; // k=18, i=2, j=1, s=0
L05: j<i; // TRUE
L06: if(i%j==0) // TRUE
L07: s=s+j: // k=18, i=2, j=1, s=1
L05: j++) // k=18, i=2, j=2, s=0
L05: j<i; // FALSE
and so on ....
I takes quite some time but you should soon see the pattern and there by understand how for-loops works.
Another thing that might help you understanding for-loops are to realize that
for(i=0; i<N; i++)
{
code...
}
is equivalent to
i=0;
while (i<N)
{
code...
i++;
}
BTW:
Always check the return value from scanf - like:
if (scanf("%d", &k) != 1)
{
printf("Input error! Program terminates.\n");
exit(1);
}
Answer from 4386427 on Stack OverflowNested for loops in C language - Stack Overflow
c++ - How nested for loops work? - Stack Overflow
I can write nested loops and they work, but I feel like I'm missing the deeper intuition. I’m hoping to hear from others who’ve had that “aha!” moment with nested loops.
Can someone please explain nested loops / loops in general, like I'm five? (C++)
Videos
I'm in a beginner class at my high school, and I'm trying to learn programming. I find loops sort of easy to understand, but we're supposed to create a multiplication table using a nested loop and I just can't get it. Help! Thank you!
I think the best way is to step through the code manually and write down the line number of the executing code and how the variables change.
Like
L01: int k,i,j,s; // k=?, i=?, j=?, s=?
L02: scanf("%d", &k); // k=18, i=?, j=?, s=?
L03: for(i=1; // k=18, i=1, j=?, s=?
L03: i<=k; // TRUE
L04: s=0; // k=18, i=1, j=?, s=0
L05: for(j=1; // k=18, i=1, j=1, s=0
L05: j<i; // FALSE
L03: i++) // k=18, i=2, j=1, s=0
L03: i<=k; // TRUE
L04: s=0; // k=18, i=2, j=1, s=0
L05: for(j=1; // k=18, i=2, j=1, s=0
L05: j<i; // TRUE
L06: if(i%j==0) // TRUE
L07: s=s+j: // k=18, i=2, j=1, s=1
L05: j++) // k=18, i=2, j=2, s=0
L05: j<i; // FALSE
and so on ....
I takes quite some time but you should soon see the pattern and there by understand how for-loops works.
Another thing that might help you understanding for-loops are to realize that
for(i=0; i<N; i++)
{
code...
}
is equivalent to
i=0;
while (i<N)
{
code...
i++;
}
BTW:
Always check the return value from scanf - like:
if (scanf("%d", &k) != 1)
{
printf("Input error! Program terminates.\n");
exit(1);
}
the inner loop is executed k time;
- when i=1 nothing happens
- when i=2 the inner loop checks if 2 is abundant or not
- when i=3 the inner loop checks if 3 is abundant or not
and so on. Maybe it would be better to rewrite your code so that variable names explains its meaning.
in what situation would you need it
generally you would require a nested for loop when
you have two (or more) dimensions to work with
for e.g : working with images, x and y dimensions
working with matrix
explanation of your code:
first you select a row to work with,
then in that selected row, you traverse all the columns
then you move on the second row
the outer loop takes care of moving to the second row, when all the columns of the first row have been processed/read
the inner for loop traverses the columns in the row selected by the outer loop
logic behind it:
the outer loop waits for the inner loop to finish, then moves on to its own next iteration.
i.e. for every iteration of the outer loop, the inner loop will cover its whole iterations.
as in your case, for every iteration of your outer loop the inner loop runs for 4 times (number of columns in each row)
and this process happens for all the rows, i.e. three times (number of rows)
A nested loop through a 2D array means traversing each element belongs to a columns and repeat that for each row.
Explanation:
