Thread: multiple condition for single variable in single line

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    17

    multiple condition for single variable in single line

    How to one variable against multiple condition ?
    Code:
    if x != 4 OR 6 OR 7 OR 13
       printf ("x")

  2. #2
    Registered User
    Join Date
    Aug 2015
    Posts
    17
    Ok, i found
    Code:
    if ((i!=5) && (i!=7) && (i!=9))

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,237
    You might find this equivalent version closer to your initial train of thought:
    Code:
    if (!(i == 5 || i == 7 || i == 9))
    Sometimes, though not likely in either of your examples, it may make sense to do a bit of arithmetic:
    Code:
    if (!(i >= 5 && i <= 9 && i % 2 != 0))
    Then, if you find yourself checking the same integer variable repeatedly in an if-else chain, e.g.,
    Code:
    if (i == 0 || i == 1)
    {
        /* #1 */
    }
    else if (i == 2 || i == 3 || i == 5)
    {
        /* #2 */
    }
    else if (i == 4 || i == 6 || i == 7)
    {
        /* #3 */
    }
    else
    {
        / * #4 */
    }
    It may make sense to convert it to a switch, e.g.,
    Code:
    switch (i)
    {
    case 0:
    case 1:
        /* #1 */
        break;
    case 2:
    case 3:
    case 5:
        /* #2 */
        break;
    case 4:
    case 6:
    case 7:
        /* #3 */
        break;
    default:
        /* #4 */
    }
    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. reading multiple strings from single row....
    By satty in forum C Programming
    Replies: 13
    Last Post: 08-06-2010, 07:39 AM
  2. Multiple timers on single window
    By csonx_p in forum Windows Programming
    Replies: 16
    Last Post: 06-25-2008, 09:06 PM
  3. multiple characters within single quotes
    By BattlePanic in forum C Programming
    Replies: 4
    Last Post: 05-06-2008, 02:59 AM
  4. Single API for multiple CGI
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 10-09-2001, 06:31 AM
  5. changing from multiple-doc to single-doc
    By swordfish in forum C++ Programming
    Replies: 2
    Last Post: 08-31-2001, 07:52 PM