Hi,
Is there such a thing as an OR statement in C?
For example:
If (x>0 OR x<10)
then.....
Thanks!
Hi,
Is there such a thing as an OR statement in C?
For example:
If (x>0 OR x<10)
then.....
Thanks!
Yes, replace the OR with || (AND is && in C)
Thank you!
No problem, I'll bill you later.
Keylac, I assume you are a beginner (so am I). Pay attention to the double symbols: && and ||
You also have the single | and &, which are the bitwise operators for or and and.
You'll remember from boolean logics:
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
0 | 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
0 & 0 = 0
It's an often made beginner's mistake to confuse these operators (logical and bitwise), it 's comparable to
if (x = 0)
another thing ....
x=6 is often mistaken with x ==6.
x = 6 means, make x equal 6.
x == 6 means, "Is x equal to 6?"
I totally agree with this point, and this is a very good point to mention where most beginner does the mistake. in condition or loop people say something like x=6 means it will, always be true.
so to reduce what we can do just
do like this
Then when ever by mistake we do likeCode:if (6 ==x)
it means the compiler will say thanCode:if (6 =x)
In my point of view we can do like this alsoerror: lvalue required as left operand of assignment
Thanks everyone for the great info!
Okay, now that you got the important, here's some fun trivia:
If your compiler supports the 1999 edition of the C standard sufficiently, you can #include <iso646.h> and use the or macro instead of ||. (<iso646.h> defines or to be || for you.) That said, there is probably no benefit for you in doing this.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)