OR statement in C

This is a discussion on OR statement in C within the C Programming forums, part of the General Programming Boards category; Hi, Is there such a thing as an OR statement in C? For example: If (x>0 OR x<10) then..... Thanks!...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    5

    OR statement in C

    Hi,

    Is there such a thing as an OR statement in C?

    For example:

    If (x>0 OR x<10)
    then.....

    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, replace the OR with || (AND is && in C)

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    Thank you!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No problem, I'll bill you later.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    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)

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    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?"

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by x2x3i5x View Post
    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

    Code:
    if (6 ==x)
    Then when ever by mistake we do like

    Code:
    if (6 =x)
    it means the compiler will say than
    error: lvalue required as left operand of assignment
    In my point of view we can do like this also

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    5

    Thanks!

    Thanks everyone for the great info!

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,629
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 06:18 AM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21