Multiple conditions in a C 'for' loop

前端 未结 7 1945
一整个雨季
一整个雨季 2020-12-15 04:19

I came across this piece of code. I generally use \'&&\' or \'||\' to separate multiple conditions in a for loop, but this code uses commas to do that.<

相关标签:
7条回答
  • 2020-12-15 04:37

    Do not use this code; whoever wrote it clearly has a fundamental misunderstanding of the language and is not trustworthy. The expression:

    j >= 0, i <= 5
    

    evaluates "j >= 0", then throws it away and does nothing with it. Then it evaluates "i <= 5" and uses that, and only that, as the condition for ending the loop. The comma operator can be used meaningfully in a loop condition when the left operand has side effects; you'll often see things like:

    for (i = 0, j = 0; i < 10; ++i, ++j) . . .
    

    in which the comma is used to sneak in extra initialization and increment statements. But the code shown is not doing that, or anything else meaningful.

    0 讨论(0)
  • 2020-12-15 04:37

    Wikipedia tells what comma operator does:

    "In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type)."

    0 讨论(0)
  • 2020-12-15 04:40

    The comma operator evaluates all its operands and yields the value of the last one. So basically whichever condition you write first, it will be disregarded, and the second one will be significant only.

    for (i = 0; j >= 0, i <= 5; i++)
    

    is thus equivalent with

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

    which may or may not be what the author of the code intended, depending on his intents - I hope this is not production code, because if the programmer having written this wanted to express an AND relation between the conditions, then this is incorrect and the && operator should have been used instead.

    0 讨论(0)
  • 2020-12-15 04:43

    Completing Mr. Crocker's answer, be careful about ++ or -- operators or I don't know maybe other operators. They can affect the loop. for example I saw a code similar to this one in a course:

    for(int i=0; i++*i<-1, i<3; printf(" %d", i));
    

    The result would be $ 1 2$. So the first statement has affected the loop while the outcome of the following is lots of zeros.

    for(int i=0; i<3; printf(" %d", i));
    
    0 讨论(0)
  • 2020-12-15 04:54

    Of course it is right what you say at the beginning, and C logical operator && and || are what you usually use to "connect" conditions (expressions that can be evaluated as true or false); the comma operator is not a logical operator and its use in that example makes no sense, as explained by other users. You can use it e.g. to "concatenate" statements in the for itself: you can initialize and update j altogether with i; or use the comma operator in other ways

    #include <stdio.h>
    
    int main(void)  // as std wants
    {
      int i, j;
    
      // init both i and j; condition, we suppose && is the "original"
      // intention; update i and j
      for(i=0, j=2; j>=0 && i<=5; i++, j--)
      {
           printf("%d ", i+j);
      }
      return 0;        
    }
    
    0 讨论(0)
  • 2020-12-15 04:58

    The comma expression takes on the value of the last (eg. right-most) expression.

    So in your first loop, the only controlling expression is i<=5; and j>=0 is ignored.

    In the second loop, j>=0 controls the loop, and i<=5 is ignored.


    As for a reason... there is no reason. This code is just wrong. The first part of the comma-expressions does nothing except confuse programmers. If a serious programmer wrote this, they should be ashamed of themselves and have their keyboard revoked.

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