Boolean Logic Question

This is a discussion on Boolean Logic Question within the C++ Programming forums, part of the General Programming Boards category; Ok... what am I missing? I just don't get why the following code doesn't work. What my code is trying ...

  1. #1
    Unregistered
    Guest

    Boolean Logic Question

    Ok... what am I missing? I just don't get why the following code doesn't work. What my code is trying to do is say, 'if the variable is equivalent to a list of possible results, print match. Otherwise, print no match.'

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    
       int x = 5;
    
       if( x == (5 || 3 || 2 || 1 || 0) )
          cout << "match" << endl;
       else
          cout << "no match" << endl;
    
       system("PAUSE");
    
       return 0;
    
    }

  2. #2
    Unregistered
    Guest
    PS I know I can fix this with something like:

    if( (x == 5 || (x == 4) || ... etc) )

    but that's kind of ugly. What are the alternatives?

  3. #3
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    
       int x = 5;
    
       switch (x)   // http://www.cprogramming.com/tutorial/lesson5.html
       {
              case 5:
                   cout << "match" << endl;
                   break;
              default:
                   cout << "no match" << endl;
                   break;
       }
    
       system("PAUSE");
    
       return 0;
    
    }
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    
       int x = 5;
       int match = 0;
       for (int i = 0; i < 6; i++)
       {
          if ( x == i )
          {
              cout << "match" << endl;
              match = 1;
          }
       }
       if (match == 0)
       {    
            cout << "no match" << endl;
       }
       system("PAUSE");
    
       return 0;
    
    }
    Last edited by Dual-Catfish; 04-14-2002 at 02:27 PM.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    559

    Re: Boolean Logic Question

    Originally posted by Unregistered
    Ok... what am I missing? I just don't get why the following code doesn't work. What my code is trying to do is say, 'if the variable is equivalent to a list of possible results, print match. Otherwise, print no match.'

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    
       int x = 5;
    
       if( x == (5 || 3 || 2 || 1 || 0) ) // if (x <= 5  && x != 4) will work
          cout << "match" << endl;
       else
          cout << "no match" << endl;
    
       system("PAUSE");
    
       return 0;
    
    }

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    I'm pretty sure that the original poster needs assistance for the problem in general of detecting equality of a variable and at least one member in a set. If that is true then the above methods probably won't help a whole lot, but then again, as far as I know, nothing will.
    I go to encounter for the millionth time the reality of experience and to forge in the smithy of my soul the uncreated conscience of my race.

    Windows XP consists of 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company, that can't stand 1 bit of competition.

  7. #7
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Let's say we have a global array of 10 elements. If the array is sorted, then you can use binary search (I belive that's what it's called) which checks halfway through the array. If the value there is more than the number, it simply ignores the top half of the array and performs the action on the lower half until the number is found. Vicaversa, if the number is above the midway point, it ignores the lower half and recursively checks the upper half until the number is found.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boolean question
    By stanlvw in forum C Programming
    Replies: 4
    Last Post: 11-15-2007, 09:19 AM
  2. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  3. Quick question on Boolean operators
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 10:43 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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