cfor-loopwhile-loop

Why is n mod n = n, instead of 0 in for loop, but correctly 0 in while loop?


I have constructed a for loop that I though would given me a "circular" index that starts at some number (n) and cycles around again to that number through the series 0 to n, that is "mod n".

Here is the code:

    n = 5
    
    for ( int i = 0; i < n; i++ )
    {

        for (int j = i + 1; j != i; j = ( (j + 1) % n ) )
        {
             printf ( "i = %i, j = %i\n", i, j );
        }

        printf("\n");
    }

Here is the output:

i = 0, j = 1
i = 0, j = 2
i = 0, j = 3
i = 0, j = 4

i = 1, j = 2
i = 1, j = 3
i = 1, j = 4
i = 1, j = 0

i = 2, j = 3
i = 2, j = 4
i = 2, j = 0
i = 2, j = 1

i mod n = 4
i = 3, j = 4
i = 3, j = 0
i = 3, j = 1
i = 3, j = 2

**i = 4, j = 5**
i = 4, j = 1
i = 4, j = 2
i = 4, j = 3

Note that the first j of the last i is 5, not 0.

I substituted the inner for loop with what I think is the equivalent while loop:

    n = 5
    
    for ( int i = 0; i < n; i++ )
    {
        int j;

        j = ( (i + 1) % n );

        while ( i != j)
        {
            printf ( "i = %i, j = %i\n", i, j );
            j = ((j + 1) % n);

        }

        printf("\n");
    }

I now get what I expected - not the first j of the last i is now 0.

i = 0, j = 1
i = 0, j = 2
i = 0, j = 3
i = 0, j = 4

i = 1, j = 2
i = 1, j = 3
i = 1, j = 4
i = 1, j = 0

i = 2, j = 3
i = 2, j = 4
i = 2, j = 0
i = 2, j = 1

i = 3, j = 4
i = 3, j = 0
i = 3, j = 1
i = 3, j = 2

i = 4, j = 0
i = 4, j = 1
i = 4, j = 2
i = 4, j = 3

What is wrong with the for loop? Aren't the for and while loop equivalent? Why don't they produce the same output?

I constructed an "inner" for loop that advanced based on using the mod function from 0 to n using the mod function. I started the for loop at varying points in the sequence, and expected it to "wrap around" through zero back to the starting point. On the last iteration, it starts at n, rather than zero, as expected.

I constructed what I believe to be the equivalent while loop to replace the inner for loop. This worked as I expected it to. Since they are (I believe) equivalent, what is wrong with the for loop?


Solution

  • That seems just to be a little mistake, in the for loop example you wrote for (int j = i + 1; j != i; j = ( (j + 1) % n ) ), which sets j to i + 1 before the loop begins instead of using j = ( (i + 1) % n ); as in the while loop example, If you do that, the outputs are the same:

    int n = 5;
        
        for ( int i = 0; i < n; i++ )
        {
    // just below
            for (int j = ( (i + 1) % n ); j != i; j = ( (j + 1) % n ) )
            {
                 printf ( "i = %i, j = %i\n", i, j );
            }
    
            printf("\n");
        }
      printf("----------------------------\n\n");
     for ( int i = 0; i < n; i++ )
        {
            int j;
    
            j = ( (i + 1) % n );
    
            while ( i != j)
            {
                printf ( "i = %i, j = %i\n", i, j );
                j = ((j + 1) % n);
    
            }
    
            printf("\n");
        }
    

    and get:

    i = 0, j = 1
    i = 0, j = 2
    i = 0, j = 3
    i = 0, j = 4
    
    i = 1, j = 2
    i = 1, j = 3
    i = 1, j = 4
    i = 1, j = 0
    
    i = 2, j = 3
    i = 2, j = 4
    i = 2, j = 0
    i = 2, j = 1
    
    i = 3, j = 4
    i = 3, j = 0
    i = 3, j = 1
    i = 3, j = 2
    
    i = 4, j = 0
    i = 4, j = 1
    i = 4, j = 2
    i = 4, j = 3
    
    ----------------------------
    
    i = 0, j = 1
    i = 0, j = 2
    i = 0, j = 3
    i = 0, j = 4
    
    i = 1, j = 2
    i = 1, j = 3
    i = 1, j = 4
    i = 1, j = 0
    
    i = 2, j = 3
    i = 2, j = 4
    i = 2, j = 0
    i = 2, j = 1
    
    i = 3, j = 4
    i = 3, j = 0
    i = 3, j = 1
    i = 3, j = 2
    
    i = 4, j = 0
    i = 4, j = 1
    i = 4, j = 2
    i = 4, j = 3