Compound relational operators in C

后端 未结 5 517
醉梦人生
醉梦人生 2020-12-22 14:10

I am trying to convert a piece of pseudo code into a C code and I have conditions like

if (-4 <= X <=8)
THEN {Do Something}
else
{Do something else}
<         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-22 14:51

    Yes you can have constants as the left hand argument of a logical check in C.

    However the pseudocode you have listed would need to be split into two expressions:

    if ((-1 <= X) && (X <= 8))

    Side Note: Many developers prefer the "constant on the left" style of logical statement because it will cause a compilation error in certain error-prone comparisons. For example: Let's say you wanted to evaluate if (X == 3) but accidentally typed if (X = 3). The latter is a perfectly valid C expression because the assignment operation returns True. If you were to use the "constant on the left" style: if (3 = X) would cause a compilation error, thus save a lot of time.

提交回复
热议问题