Thread: Multi-conditional if statements.

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079

    Multi-conditional if statements.

    Is there a way to do them?

    For example:

    Code:
    if (x=1 or y=2 or z=3)
    {
    do something
    }
    else
    {
    return=0;
    }
    Thanks for any help you can give.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Code:
    if (x==1 || y==2 ||  z==3)
    {
    do something
    }
    else
    {
    return 0;
    }
    You use || as or, && is and.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    also, know that && (AND) is figured before || (OR)

    for example: if(true && false || true) evaluates to true

    there are also other operators, like the ! (NOT) operator. it simply negates the value. for example, if(!true) evaluates to false.

    you can also use parenthesis. I assume you know how parenthesis mess with the order of operations in arithmetic, so all I'm going to say is it's essentially the same for logic.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    be careful though

    c++ uses short circuit evaluation

    take for example
    Code:
    bool thisIsTrue = true;
    if(thisIsTrue || ImportantFunction())
    {
        .........
    }

    when it sees the || it knows that either the left operand (thisIsTrue) or the right operand (the return value of ImportantFunction()), must be true in order to execute the code within the if{} block. since "thisIsTrue" is in fact true, there is no need to evaluate the rest of the expression and does not call "ImportantFunction()".



    don't get cute with that piece of information either and do
    Code:
    //bad
    if(x || func())
    in place of
    Code:
    //good
    if(!x)
       func();
    Last edited by misplaced; 03-23-2005 at 04:30 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Thanks for this info guys.

    I saw info exactly like this on the internet, except it was refering to PHP. The statements looked exactly like C++, but I wanted to ask just to be sure.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Also... since I'm thinking about things I've seen in other languages.

    In strings, can you make an if statement that looks to see if the user's input is contained in your criteria but isn't exactly your criteria?

    For instance, if my statement is looking for the user to say "North" but they choose to say "Nor" or "N" it would accept that so long as it doesn't interfere with other criteria.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    22
    Quote Originally Posted by major_small
    also, know that && (AND) is figured before || (OR)

    for example: if(true && false || true) evaluates to true
    That example evaluates to true regardless of which operator has precedence.

    ( (true && false) || true ) == true
    ( true && (false || true) ) == true

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    17
    If you use -Wall (as you really should) when compiling the compiler will warn you if you forgot the second = in the confrontation.
    Also, if you are afraid to make mistakes, just use the constant in front:
    if (0 == p)
    so, if you forgot a equal the compiler will tell that the assignement is not valid.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (0 == p)
    Oddly, it seems easier to remember to use == than remember to swap the variables round.

    Not only is p == 0 the natural way of reading it, because you've remembered that it should be == and not =, you DON'T have a problem when you try and do something like
    if ( p == q )

    Rearranging it to
    if ( q == p )
    doesn't help you because both can be assigned and your syntactic crutch to stop you falling over no longer works as it should.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    In strings, can you make an if statement that looks to see if the user's input is contained in your criteria but isn't exactly your criteria?
    Assuming you're talking about std::string and not char[]:
    Code:
    if(str.find("No") != std::string::npos)
       //it's contained.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    also, know that && (AND) is figured before || (OR)
    NOT true. The order of evaluation is always left to right in a boolean statement. && binds the results tighter then ||.

    So if you have:
    Code:
    if ( x==5 || y==2 && z==7)
    the x==5 is evaluated before the rest of it.

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Thantos
    NOT true. The order of evaluation is always left to right in a boolean statement. && binds the results tighter then ||.

    So if you have:
    Code:
    if ( x==5 || y==2 && z==7)
    the x==5 is evaluated before the rest of it.
    I think you misunderstood what I meant. I meant that && has a higher precedence than ||. bad wording on my part. and a bad example as well. (thanks to skiingwiz for pointing that out). with that in mind, here's a better example:

    if(false && true || true) evalutates to true
    if((false && true) || true) evaluates to true
    if(false && (true || false)) evaluates to false


    C operator precedence:
    http://msdn.microsoft.com/library/de...m/expre_13.asp
    Last edited by major_small; 03-24-2005 at 12:06 AM. Reason: linkage
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Still a bad example This also demonstrates the left-to-right evaluation order (evaluates the && first, because it is the leftmost operator).

    **EDIT**
    Hmm, I can't seem to come up with an example that will make a distinction between the two. Could it be that the two will always just give you the same result? In which case, the only real way to find out the order of evaluation would be to create bool functions that give output when they are called.
    Last edited by Hunter2; 03-24-2005 at 12:16 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Hunter2
    Still a bad example This also demonstrates the left-to-right evaluation order (evaluates the && first, because it is the leftmost operator).
    fine, how's this:

    if(!(!A && B || A && !B))

    jeebus... and 10 points to anybody whow knows what that gate's called.

    edit: and another 20 points to the person who brings me the head of the person that gives me bad rep for trying to be helpful.
    Last edited by major_small; 03-24-2005 at 12:48 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    just a little note to circuit evaluation:
    it makes it possible to write code like:

    Code:
    int *p;
    /* code */
    if(NULL != p && 1 == *p) {
    
    }
    without circuit evaluation (1 == *p) would be evaluated even though p could be NULL.
    signature under construction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Conditional Statements
    By strokebow in forum C Programming
    Replies: 30
    Last Post: 11-22-2006, 06:08 PM
  3. multi conditional if statements?
    By chaser in forum Game Programming
    Replies: 3
    Last Post: 07-26-2002, 10:52 PM
  4. Using floats in conditional statements
    By DBB in forum C++ Programming
    Replies: 3
    Last Post: 09-12-2001, 07:54 AM