-2
        int inc = swap ? 1 : -1;
        for(int j=j1; j!=j2; j+=inc){
            if(j < 0)
                j = curve2->controlPoints()->size()-1;
            if(j >= curve2->controlPoints()->size())
                j = 0;
            curve->addControlPoint(curve2->controlPoint(j)->pos(), curve2->controlPoint(j)->triangle());
        }

I found out that in some case, this for loop infinitely. When looking with a debugger, j does reach j2 but for some reason continue to loop.

I then tried to add a break if j == j2 inside the loop (technically j-inc since j is incremented as it enter into the loop again)

    for(int j=j1; j!=j2; j+=inc){
            if (j - inc == j2)
            {
                qDebug() << "break =================================";
                break;
            }
            if(j < 0)
                j = curve2->controlPoints()->size()-1;
            if(j >= curve2->controlPoints()->size())
                j = 0;
            curve->addControlPoint(curve2->controlPoint(j)->pos(), curve2->controlPoint(j)->triangle());
        }

And doing that indeed solved the problem (and the "break" is indeed printed), but it doesn't really make any sense ? Why does the first for loop act this way ?

Edit : I'm iterating over a part of a list (between values j1 and 2). The iteration can go both side depending of the swap parameter (boolean). If j reach one of the end of the list, it continue on the other side (for example if j1=5, j2=1 and the list size is 7, j will take the following values : 5 6 0 1)

collax
  • 39
  • 4
  • 2
    You modify `j` inside the loop. What happens if that modification leads to `j != j2` *never* being true? – Some programmer dude Mar 08 '19 at 12:56
  • 1
    And what *are* the values of `j1`, `j2` and `swap`? What possible values do you assign to `j` inside the loop? Will any function you call modify `j2`? Please create a [mcve] to show us. And please use a debugger to check the actual values as you iterate in the loop. – Some programmer dude Mar 08 '19 at 12:57
  • As i said i used a debugger to check the actual values as i iterated the loop and j does become equal to j2 but it still didnt leave the loop. – collax Mar 08 '19 at 13:06
  • 2
    we dont see the output of you debugger, hence you need to show us a complete example. We cannot fix code that we dont see. Read about [mcve] – 463035818_is_not_an_ai Mar 08 '19 at 13:13
  • 1
    It seems like you believe that the incrementation happens *after* the condition test, but the order is actually the opposite. – molbdnilo Mar 08 '19 at 13:15
  • @molbdnilo actually no, OP checks for `j - inc == j2` being aware that in the next iteration `j` will be first incremented and then compared to `j2`, on the other hand, I have no idea. I find the question rather unclear – 463035818_is_not_an_ai Mar 08 '19 at 13:20

1 Answers1

4

It should be noted that I'm only guessing about what happens here...

My guess is that j becomes equal to j2 inside the loop, by one of the assignments. But then the increase j += inc happens and j is no longer equal to j2 when the loop condition is checked.


Generally speaking, a for loop is equivalent to a while loop:

for (a; b; c)
    d

is equivalent to

{
    a;
    while (b)
    {
        d;

        c;
    }
}

That means your first loop is equal to (with extra comments added)

{
    int j = j1;

    while (j != j2)
    {
        if(j < 0)
            j = curve2->controlPoints()->size()-1;
        if(j >= curve2->controlPoints()->size())
            j = 0;
        curve->addControlPoint(curve2->controlPoint(j)->pos(), curve2->controlPoint(j)->triangle());

        // At this point `j == j2`, so the loop condition is false

        // BUT then you do
        j += inc;

        // Here `j != j2` again, and the loop condition is true and will continue
    }
}

Perhaps your loop condition should be j - inc == j2 instead?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621