Compound relational operators in C

后端 未结 5 503
醉梦人生
醉梦人生 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:59

    In C, you cannot write a condition like

    if (-4 <= X <= 8) {
         // ...
    } else {
         // ...
    }
    

    Instead, you will have to split this into two separate checks:

    if (-4 <= X && X <= 8) {
         // ...
    } else {
         // ...
    }
    

    This code is now totally fine - you can have whatever operands you'd like on either side of the <= operator.

提交回复
热议问题