Multiple conditions in a C 'for' loop

前端 未结 7 2150
一整个雨季
一整个雨季 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: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.

提交回复
热议问题