Thread: Question About Or Statements

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Question About Or Statements

    Hi,

    I have a couple of questions, if anyone wouldn't mind answering them.

    1. If you compile the following program, a is recognised by the first if condition, but not the second. Can anyone explain why this is? It seems like in the second condition, (12 || 17 || 31 || 44) should just equate to either 1 or 0 like the first condition.

    2. If you can't use something like the second if statement, is there a more efficient way to check whether a variable is one of several different values than via something like the first one?

    Thanks.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int a = 12;
        if ((a == 12) || (a == 17) || (a == 31) || (a == 44))
            cout << "a recognised by condition 1" << endl;
        if (a == (12 || 17 || 31 || 44))
            cout << "a recognised by condition 2" << endl;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, you said the reason. 12||17||31||44 is, in fact 1 (since 12 is true, and 17 is true, and 31 is true, and 44 is true, and true or true is true, and true is 1 -- || will always return either 0 or 1). a==1 is false, because a is in fact 12.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bengreenwood
    1. If you compile the following program, a is recognised by the first if condition, but not the second. Can anyone explain why this is? It seems like in the second condition, (12 || 17 || 31 || 44) should just equate to either 1 or 0 like the first condition.
    The expression (12 || 17 || 31 || 44) evaluates to true, but true is converted to 1, and 12 is not equal to 1.

    Quote Originally Posted by bengreenwood
    2. If you can't use something like the second if statement, is there a more efficient way to check whether a variable is one of several different values than via something like the first one?
    The first if statement seems pretty efficient for what you want to do. Depending on the situation you may be able to check if the value lies within a range, e.g., (a >= 12 && a <= 44). Or maybe you can have a std::set of possible values and then attempt to find if the given value is within the set.

    EDIT:
    Quote Originally Posted by tabstop
    || will always return either 0 or 1
    Strictly speaking, the type of the result of the built-in operator|| is a bool. What happens here is an integral promotion from bool to int.
    Last edited by laserlight; 06-04-2009 at 12:38 PM.
    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

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by bengreenwood View Post
    2. If you can't use something like the second if statement, is there a more efficient way to check whether a variable is one of several different values than via something like the first one?
    Yes, yes there is. However it only works on a limited range of values. E.g. for a 32-bit int we can test for any and all values up to 31...
    Code:
    if ((1 << a) & ((1<<12) | (1<<17) | (1<<31)))
    You could include the 44 if you do the same thing with 64 bit values.
    I never said it was pretty, but hot-dam it's fast!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While Statements Question
    By thekautz in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2008, 02:48 PM
  2. Multiple If Statements Question.
    By thekautz in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2008, 03:06 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM