Is this code valid to test if "x" is equal to either condition? if(x == (CONDITION1 || CONDITION2)) { //stuff } The compiler (Codesourcery GCC) seems to be ok with it, but is it legal/reliable code?
Having said that: It is valid in the sense, that it follows the language rules. But it does not what you think it should do.
if ( (x == CONDITION1) || (x == CONDITION2) ) { //stuff } The 'stuff' section will be processed if one of the conditions is true.
this
1 | if(x == (CONDITION1 || CONDITION2)) { |
2 | |
3 | //stuff
|
4 | }
|
is equal to this
1 | if((x == CONDITION1) || (x == CONDITION2)) { |
2 | |
3 | //stuff
|
4 | }
|
(only) when
1 | BOOL x = YES; // must be YES |
2 | |
3 | BOOL CONDITION1 = ...; |
4 | BOOL CONDITION2 = ...; |
Thanks to everyone for the lightning quick replies. I've always used: if( ( x == CONDITION1 ) || ( x == CONDITION2 ) ) but was hoping to use a #define to turn this: if(x == CONDITION) into what I proposed by doing something like: #define CONDITION (CONDITION1 || CONDITION2) I'll just go through the code and modify each instance correctly :)
The double vertical bar "||" is a relational operator like "<" or ">". The single vertical bar "|" is a bit-wise operator. You can use the the single vertical bar to do what you were trying to do in the original post. The bit-wise combination of constants will yield a constant that can be used to test multiple conditions simultaneously.
So, to answer your question "Is this C statement valid?": Yes it is. But it works differently like you expect. The language C don't understand booleans. It takes numbers to represent true or false. false == 0; true != 0; // common 1 Your statement:
1 | if(x == (CONDITION1 || CONDITION2)) |
2 | {
|
3 | |
4 | //stuff
|
5 | }
|
checks first the statement within the parenthesizes. It returns a 1 when CONDITION1 OR CONDITION2 is not 0. Otherwise it returns a 0. Then it checks x with the return value. When x is 0 and both conditions are also 0 the equation returns a 1. When x is 1 and at least one condition is not 0 the equation returns a 1, too. When the expression in the if-header is not 0 the if branch will be executed. Otherwise not, of course. All this does not hurt the rules of C. ____________________________ But you purpose was to check if x has the same value like CONDITION1 OR CONDITION2. To do this you have to reorder your question. Has x the same value like CONDITION1 OR has x the same value like CONDITION2? Therefore is the answer of kbuchegg and the others correct.
1 | if( ( x == CONDITION1 ) || ( x == CONDITION2 ) ) |
2 | {
|
3 | //stuff
|
4 | }
|
This expression returns a 1 when at least one of the statements within the parenthesizes return a 1. regards BrEin ************************************* I hope this answer is not "overdressed" and I seam to be a squirt. I thought the question was caused by missing some knowledge. Sorry if not.
Fabian Hoemcke write: > What is right? > a 1? > or > an 1? > > I serios don't know it? :D Since 1 ("one") doesn't start with a spoken vowel, it's "a 1" and not "an 1". But in this context > ... the equation returns a 1. I think it's better style to omit the article: ... the equation returns 1. Klaus Wachtler write: > there is only one 1 > :-) I have at least three of them. Would you like to see them? 1 1 1 ;-) SNCR
Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
Log in with Google account
No account? Register here.