How do I put two increment statements in a C++ 'for' loop?

前端 未结 8 670
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 02:55

I would like to increment two variables in a for-loop condition instead of one.

So something like:

for (int i = 0; i != 5; ++i and ++j)          


        
相关标签:
8条回答
  • 2020-11-28 03:38
    for (int i = 0; i != 5; ++i, ++j) 
        do_something(i, j);
    
    0 讨论(0)
  • 2020-11-28 03:39

    Use Maths. If the two operations mathematically depend on the loop iteration, why not do the math?

    int i, j;//That have some meaningful values in them?
    for( int counter = 0; counter < count_max; ++counter )
        do_something (counter+i, counter+j);
    

    Or, more specifically referring to the OP's example:

    for(int i = 0; i != 5; ++i)
        do_something(i, j+i);
    

    Especially if you're passing into a function by value, then you should get something that does exactly what you want.

    0 讨论(0)
  • 2020-11-28 03:42

    Try not to do it!

    From http://www.research.att.com/~bs/JSF-AV-rules.pdf:

    AV Rule 199
    The increment expression in a for loop will perform no action other than to change a single loop parameter to the next value for the loop.

    Rationale: Readability.

    0 讨论(0)
  • 2020-11-28 03:44

    I agree with squelart. Incrementing two variables is bug prone, especially if you only test for one of them.

    This is the readable way to do this:

    int j = 0;
    for(int i = 0; i < 5; ++i) {
        do_something(i, j);
        ++j;
    }
    

    For loops are meant for cases where your loop runs on one increasing/decreasing variable. For any other variable, change it in the loop.

    If you need j to be tied to i, why not leave the original variable as is and add i?

    for(int i = 0; i < 5; ++i) {
        do_something(i,a+i);
    }
    

    If your logic is more complex (for example, you need to actually monitor more than one variable), I'd use a while loop.

    0 讨论(0)
  • 2020-11-28 03:49
    int main(){
        int i=0;
        int a=0;
        for(i;i<5;i++,a++){
            printf("%d %d\n",a,i);
        } 
    }
    
    0 讨论(0)
  • 2020-11-28 03:52

    A common idiom is to use the comma operator which evaluates both operands, and returns the second operand. Thus:

    for(int i = 0; i != 5; ++i,++j) 
        do_something(i,j);
    

    But is it really a comma operator?

    Now having wrote that, a commenter suggested it was actually some special syntactic sugar in the for statement, and not a comma operator at all. I checked that in GCC as follows:

    int i=0;
    int a=5;
    int x=0;
    
    for(i; i<5; x=i++,a++){
        printf("i=%d a=%d x=%d\n",i,a,x);
    }
    

    I was expecting x to pick up the original value of a, so it should have displayed 5,6,7.. for x. What I got was this

    i=0 a=5 x=0
    i=1 a=6 x=0
    i=2 a=7 x=1
    i=3 a=8 x=2
    i=4 a=9 x=3
    

    However, if I bracketed the expression to force the parser into really seeing a comma operator, I get this

    int main(){
        int i=0;
        int a=5;
        int x=0;
    
        for(i=0; i<5; x=(i++,a++)){
            printf("i=%d a=%d x=%d\n",i,a,x);
        }
    }
    
    i=0 a=5 x=0
    i=1 a=6 x=5
    i=2 a=7 x=6
    i=3 a=8 x=7
    i=4 a=9 x=8
    

    Initially I thought that this showed it wasn't behaving as a comma operator at all, but as it turns out, this is simply a precedence issue - the comma operator has the lowest possible precedence, so the expression x=i++,a++ is effectively parsed as (x=i++),a++

    Thanks for all the comments, it was an interesting learning experience, and I've been using C for many years!

    0 讨论(0)
提交回复
热议问题