Is this C statement valid?

Guest #1811677
Rate this post
• ▲ useful
▼ not useful
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
Rate this post
• ▲ useful
▼ not useful
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
Rate this post
• ▲ useful
▼ not useful
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
Rate this post
• ▲ useful
▼ not useful
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
Rate this post
• ▲ useful
▼ not useful
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

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

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.