Multiple conditions in a C 'for' loop

前端 未结 7 2153
一整个雨季
一整个雨季 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: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));
    

提交回复
热议问题