Using assignment as a condition expression?

前端 未结 5 1850
北荒
北荒 2020-11-28 13:06

Consider:

if (a=5) {
   /* do something */
}

How does the assignment work as a condition?

Is it based on non-zero value of l-value?

5条回答
  •  臣服心动
    2020-11-28 13:43

    if(a=x) is equivalent to if(x) in addition to a assigned with x. So if the expression x evaluates to a non-zero value, then if(x) simply becomes if(true). Otherwise, it becomes if(false).

    In your case, since x = 5, that means f(a=5) is equivalent to if(true) in addition to a assigned with 5.

提交回复
热议问题