What does y -= m < 3 mean?

后端 未结 8 2058
面向向阳花
面向向阳花 2021-01-30 20:03

While looking through some example C code, I came across this:

y -= m < 3;

What does this do? It it some kind of condensed for loop or som

8条回答
  •  终归单人心
    2021-01-30 20:42

    If you break it down by order of precedence for each operation, you get:

    y = (y - (m < 3));
    

    m < 3 gets evaluated and returns a boolean result 1 or 0, so the expression can be simplified as

    y = y - 1; // if m < 3 is true
    

    or

    y = y - 0; // if m < 3 is false
    

    The purpose for doing this is to avoid an if clause.

提交回复
热议问题