1

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?

2
  • 1
    You need int j = (i + 1) % n Commented Jun 14 at 21:21
  • Thank you luk2302!!! You are exactly right! Commented Jun 14 at 21:27

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

Comments

1

I figured it out. The initialization of j in the for loop is wrong, and in the while loop is right.

       for (int j = i + 1; j != i; j = ( (j + 1) % n ) )

should be

       for (int j = ( (i + 1) % n ); j != i; j = ( (j + 1) % n ) )

The initialization does not go through the increment statement (duh!) and is therefore not being mod'ed. I instinctively did it right on the while loop because the while loop.

Thanks for helping me thing this through!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.