Compound relational operators in C

我与影子孤独终老i 提交于 2019-11-30 10:01:45

问题


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}

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


回答1:


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.




回答2:


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.




回答3:


No; that won't work.

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




回答4:


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...
}



回答5:


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.)



来源:https://stackoverflow.com/questions/17031880/compound-relational-operators-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!