Is this C statement valid?

Guest #1811633
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.
Guest #1811642
if ( (x == CONDITION1) || (x == CONDITION2) )
{

    //stuff
}


The 'stuff' section will be processed if one of the conditions is true.
Guest #1811677
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 = ...;
OP #1811682
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 :)
(Company: Wadia) #1901960
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.
Guest #1902023
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.
Guest #1904783
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

Reply

A question of your own? This thread is over 16 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.