Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

qt - C++ for loop continue to loop even though it shouldn't

        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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...