Thread: multiple ifs...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    4

    multiple ifs...

    i have a an if statement is there a way to make multiple fuctions equal to one value? for example.

    Code:
    if ( x == " yes "," maybe"){
            a = 10;
    }
    or...
    if ( x == 1-10){
            b = "yes";
    }
    or something of that syntax im not sure what but is there any way to do this? or would i have to do each individual one seperatly?

    thanks for the help.

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    you would either use && or ||, and or, or respectively. in your example it would look like.

    Code:
    if ( x == " yes " ||  x ==  " maybe"){
            a = 10;
    }
    or...
    if ( x >= 1 && x <= 10){
            b = "yes";
    }

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Note that BOTH of the original code-snippets are actually valid C code. They just don't do quite what you expect. The first one is "always true" [at least if we assume the compiler doesn't place strings that have the address of "NULL" - which is true for all compilers I've ever seen].

    This is because of the fairly rare "comma operator", where you can write
    Code:
    lvalue = expr1, expr2, expr3
    lvalue will get the value of expr3, so in the case of the first code-snippet, the if-statement will be based on the " maybe" string - which is "true" by the C standard.

    The second expression compares x with -9, since 1-10 is -9.

    --
    Mats
    Last edited by matsp; 01-17-2008 at 06:09 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    4
    Quote Originally Posted by matsp View Post
    The second expression compares x with -9, since 1-10 is -9.
    Mats
    ok i was wondering if there was a way to do like:
    1 through 5 = something
    6 through 10 = something else
    etc.

    but i guess dracs way works also, well i havent tried yet but thanks for the help.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    if (x >= 1 && x <= 5) {
        something();
    }
    else if (x >= 6 && x <= 10) {
        something_else();
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  3. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM