kireinasora
kireinasora

Reputation: 383

what's the difference between these two in C?

in learning quick union algorithm, i have met these two statements

for(i=p; i!=id[i]; i=id[i]);
for(j=q; j!=id[j]; j=id[j]);

since I only learned for loop to be something like

for(i=0; i<100; i++)

I don't know the difference between the two statements and the following statements

i=p; i=id[i];
j=q; j=id[j];

i have no idea why the results are different?

thanks


I want to ask why

#include <stdio.h>
#define N 10000

int main()
{
    int i, j, p, q, id[N];
    for(i=0; i<N; i++) id[i]=i;
    while(scanf("%d %d\n", &p, &q)==2)
    {
        for(i=p; i!=id[i]; i=id[i]);
        for(j=q; j!=id[j]; j=id[j]);
        if(i==j) continue;
        id[i]=j;
        printf(" %d %d\n", p, q);
    }
}

is different from

#include <stdio.h>
#define N 10000

int main()
{
    int i, j, p, q, id[N];
    for(i=0; i<N; i++) id[i]=i;
    while(scanf("%d %d\n", &p, &q)==2)
    {
        i=p; i=id[i];
        j=q; j=id[j];
        if(i==j) continue;
        id[i]=j;
        printf(" %d %d\n", p, q);
    }
}

I have tested the results, that's why I am confused

Upvotes: 2

Views: 85

Answers (2)

Julian
Julian

Reputation: 424

When looking at non-standard for "loops" like this, it helps to convert your for loops into while loops. (that is all they really are)

To do that, remember that a for loops consists of three parts separated by semicolons. The initialization part, the conditional part, and the update or increment part:

for(initialize statement; boolean loop conditional; update/increment statement);

The initialize statement is executed before your loop, the loop conditional is evaluated to determine whether the loop continues, and the update/increment statement is executed as the end of the loop.

Your first example:

for(i=p; i!=id[i]; i=id[i]);

As a while loop, looks like..

i = p
while(i!=id[i]) {
    i = id[i];
}

Your second example:

for(j=q; j!=id[j]; j=id[j]);

As a while loop, looks like..

j = q
while(j!=id[j]) {
    j = id[j];
}

Once they're written like this, it's easier to tell what's going on.

We're initializing the loop variable to one of two values, p or q.

Then, we're looking into the array "id" at the location specified by the loop variable and updating the loop variable with it. This has the effect of looking up in the array the next loop variable. In other words, each slot in the array contains the next value to jump to.

The conditional checks to see whether the destination is the same as the current location. That is to say, if we are "told" to jump to the location we're already in.

The difference between you two loops is only the initialization value. The first one initializes to p where the second initializes to q.

It may be helpful to manually jump through a simple cases such as..

p=0
q=1
id = {1,2,2}

p=2
q=1
id = {0,0,2}

Upvotes: 0

Magisch
Magisch

Reputation: 7352

i=p;

Sets i to the value of a variable p (defined and initialized elsewhere in the program)

i=id[i]

Sets i to the value of the i th element of the array id (defined and initialized elsewhere in the program)

for(i=p; i!=id[i]; i=id[i]);

Loop initializes i to the value of p, executes the statements inside the for loop once if i is not equal to the i'th value of the array id, and then stops.

Further explaination:

assuming some values for the variables:

int p = 4;
int i;
int id[5] = {1,2,3,4,5};
for(i=p; i!=id[i]; i=id[i]) {
printf("Loop executed!\n");
}

Output:

Loop executed!

And then a segmentation fault.

What happens:

i is set to 4, then compared to id[4]. This is unequal, thus the loop is triggered. After that it sets i to 5. Now it tries to compare id[5] to i. This is disallowed because id only has space for 5 elements and indexes start at 0.

Upvotes: 1

Related Questions