Home > Mobile >  Loop within a loop logic
Loop within a loop logic

Time:06-30

I am veeery new to coding. Could you explain the logic behind the loop within a loop code in C. To be more precise:

I do not get in my code the sequence of how the loops happen.

I.e if I type in the code size: 1 only one # will appear, even though there are two 'for' loops, so should not each of them print out # separately so two hashtags overall?

i.e when I type 2 the terminal gives four hashtags, which I get. Because from my logic each for loop creates two hashtags (once when each integer equals 0 and the second one when the integer get added and equals 1 (where it stops)

i.e when I type 3 the terminal gives 9 hashtags, which I do not get again, because in my mind each for loop should have give us 3 hashtags so 6 in total (integer =0 then 1 then 2)

So again, my question is how does the sequence happen here? If you could please explain one of the examples I gave I think I would get the rest.

Sorry for the long post, hopefully my question is clear.

Code:

int main(void)
{
    int n;
    do
    {
        n = get_int("Size: ");
    }
    while (n < 1);

  
    for (int i = 0; i < n; i  )
    {
      
        for (int j = 0; j < n; j  )
        {
            
            printf("#");
        }


    }
}

CodePudding user response:

When you have nested loops, for each iteration of the outer loop, the inner loop goes through all of its iterations.

So what happens is not the sum of the iterations of the loops, but the product. In your case that's 1x1=1, 2x2=4, and 3x3=9.

CodePudding user response:

This is what your code does for any value of n- it counts i up till to N and then for every i, it counts j till n. As you may see, for every n, you are again counting N times resulting in n^2 operations. Hence for n=3, you get 9 hashes.

CodePudding user response:

The essence of for loops like the ones you have here is always as follows: an index starts at 0, some code is executed until that index reaches a value. In case of nested loops like these, the code that gets executed is itself another for loop. If we take n = 3 for example, the sequence looks like this:

First iteration of the first loop: i = 0
    First  iteration of the second loop: j = 0
    Second iteration of the second loop: j = 1
    Third  iteration of the second loop: j = 2
Second iteration of the first loop: i = 1
    First  iteration of the second loop: j = 0
    Second iteration of the second loop: j = 1
    Third  iteration of the second loop: j = 2
Third iteration of the first loop: i = 2
    First  iteration of the second loop: j = 0
    Second iteration of the second loop: j = 1
    Third  iteration of the second loop: j = 2

Hope this clears things up, and welcome to the wonderful world of programming! :)

CodePudding user response:

Your confusion is because you're thinking of them as one loop after the other when instead it's one loop inside the other.

When `i` is `0`, j is going to do `0`, `1`, `2`
When `i` is `1`, j is going to do `0`, `1`, `2`
When `i` is `2`, j is going to do `0`, `1`, `2`

That makes 9.

You're doing the math of 3 3=6 but you've written code that is 3*3=9.

  • Related