Multiple conditions in a C 'for' loop

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

提交回复
热议问题