C++ and Boolean Operators

This is a discussion on C++ and Boolean Operators within the C++ Programming forums, part of the General Programming Boards category; Hello, I've recently taken on interest in C++, planning to start college and eventually aim for CS degree. Anyways, I've ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    1

    C++ and Boolean Operators

    Hello, I've recently taken on interest in C++, planning to start college and eventually aim for CS degree. Anyways, I've been checking out the tutorials and I have been trying to write a simple code using Boolean Operators.

    I kind of understand the concept but there are no actual examples in the tutorials with Boolean Operators so I'm having a tough time with this code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a;
        
        cout<<"I am thinking of a number from 1 to 15.\n";
        cin>> a;
        cin.ignore();
        if ( a == 3 && 7  ){
             cout<<"That is correct!";
             }
        else if ( a == !(3 && 7)  ){
             cout<<"Try again.";
             }
        cin.get();
    }
    When I input 3 it recognizes as TRUE but if I input 7 or any other number, I don't neither of the IF or ELSE IF statements.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    this is how it should be;
    if((a==3)||(a==7)); //or not and

    similarly change the second part.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The following is not the correct way to check if a equals 3 and 7:
    Code:
    if ( a == 3 && 7  )
    To check whether a equals 3 and 7, you would write
    Code:
    if( a==3 && a==7)
    However, when two conditionals are and'ed together, both conditionals have to be true for the whole thing to be true. So, if the user enters 3, then a==3 is true but a==7 is false, so the whole thing is false. In plain english, a cannot be both 3 and 7. a can only be 3 OR 7. When two conditionals are or'ed together, only one of them has to be true for the whole thing to be true. So, if you write:
    Code:
    if( a==3 || a==7)
    and the user enters 7, then a==3 is false and a==7 is true, so the whole thing is true, and therefore the code in the body of the if statement will execute.

    When I input 3 it recognizes as TRUE but if I input 7 or any other number, I don't neither of the IF or ELSE IF statements.
    To analyze why that happened, you have to know a couple of things about the way C++ works. Any expression that results in a value of 0 is false, and any expression that results in a value that is non-zero(e.g. -10, 1) is true. So, looking at this statement:
    Code:
    if ( a == 3 && 7  )
    the C++ precedence rules say that statement is equivalent to:
    Code:
    if(  (a==3) && 7 )
    If a is equal to 3, then a==3 is true. 7 is non-zero so it is true, so you end up with:
    Code:
    if(true && true)
    which makes the whole thing true.

    On the other hand, suppose a is equal to 7, then a==3 is false. 7 is non-zero so it is true, and you end up with:
    Code:
    if(false && true)
    which makes the whole thing false.
    Last edited by 7stud; 03-13-2006 at 04:04 AM.

  4. #4
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    Here's a little summary of how logical boolean operators work:

    The operators:
    Code:
    a == b //returns true if 'a' is equal to 'b'. this is NOT THE SAME AS 'a = b'
    a && b //returns true if both 'a' and 'b' are true, returns false in all other cases
    a || b // returns true if 'a' is true, if 'b' is true', or if both 'a' and 'b' or true. Only returns false if both 'a' and 'b' are false.
    you can replace 'a' and 'b' with any expressions which evaluate to a boolean value. These all associate left to right and are on the same level of operator precedence.

    The expression "a && b || c" is evaluated like this "(a && b) || c".

    Here's an example of how you might use boolean operators in a program:

    Code:
    if ( (a == 3) || (a ==5) )  //execute "do stuff" if a == 3 is true, a ==5 is true, or they both are true.
      //do stuff
    else //execute "do other stuff" if a != 3 AND a != 5
      //do other stuff
    the above code is the same as

    Code:
    if ( (a == 3) || (a ==5) )
      //do stuff
    if ( (a != 3) && (a != 5) ) 
      //do other stuff
    hope that helped.

Popular pages Recent additions subscribe to a feed

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