Compound relational operators in C

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

    No; that won't work.

    -4 <= X is a either 0 or 1, which is always less than 8.

    0 讨论(0)
  • 2020-12-22 14:39

    Is the syntax in if statement valid? Can a constant be placed before the variable in the logical condition to check the truth value?

    Not sure if syntax is right, but placing a literal constant before the test for equality\inequality operator is a common practice. Like:

    if(7==x) {...} else {...}
    

    Some programmers do like this, because if you accidentally forget the second '=' symbol in test for equality\inequality\greater then operator, you will receive the assignment of variable to literal, not testing, like:

    if(x=7) //danger! you're assigning to variable 'x' value of '7',
            //which will return true,  as a side-effect
    //now (x==7)==true
    

    Also, C language doesn't have 'THEN' keyword, inventors of the language decided that it's redundant - it's obviously easy to understood, that if you have something after if test, then you probably going o execute this code. like

    if(SOME_MAGIC_CONST==a) 
    {
         //There couldn't be anything else at all! else can't follow if immediately, so...
    }
    else {
         //yep, keyword  'THEN' is redundant...
    }
    
    0 讨论(0)
  • 2020-12-22 14:46

    Is the syntax in if statement valid?

    The syntax is valid, but it won't do what you expect. <= is a normal, left-associative binary infix operator in C, so

    -4 <= X <= 8
    

    parses as

    (-4 <= X) <= 8
    

    The result of <= is a Boolean value, which C represents as 1 / 0, and both 0 <= 8 and 1 <= 8 are true.

    To get the effect you want (check whether X is in a certain range), you need multiple separate comparisons combined with &&.

    Can a constant be placed before the variable in the logical condition to check the truth value?

    Yes. You can also compare two variables or two constants or pretty much anything.

    <=, <, and all other comparisons are general operators. Their operands can be any expression you want. (Syntactically, that is; for the code to make sense the two operands also must have the same type. 5 <= "abc" is a type error.)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题