Using assignment as a condition expression?

前端 未结 5 1849
北荒
北荒 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:29

    Every non-zero value will be considered as true.

    So some people will suggest you write

    5 == a
    

    to avoid that you make mistake == by =.

提交回复
热议问题