Question About Or Statements

This is a discussion on Question About Or Statements within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a couple of questions, if anyone wouldn't mind answering them. 1. If you compile the following program, ...

  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,185
    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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,288
    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, 01:48 PM
  2. Multiple If Statements Question.
    By thekautz in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2008, 02: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, 08:22 PM
  5. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM

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