Thread: Beginer trying to understan the Boolean Operators or,and,not

  1. #1
    Registered User
    Join Date
    Jul 2011

    Beginer trying to understan the Boolean Operators or,and,not

    I am very intrested in learning c++ but, when I am learning needs some explantion along with an example. I figgured that is the best way I can take in things. I started the tutorials last night and was doing great intill I got to the Boolean Operators OR,AND,NOT. I kind of get an idea but tried for awhile to put them in axtion and nothing. The tutorials I am going through can be found here If Statements in C++ - Tutorial is there any other online tutorial that will explain this better and give an example or could one of you guys give an example of these. Im not sure if this is a must thing to know in C++ but, I dont want to move on untill I fully understand it.
    Last edited by jpedersm; 07-25-2011 at 04:12 AM.

  2. #2
    Registered User
    Join Date
    Jul 2007
    What don't you understand about those operators?

    a AND b is true, if both a and b are true.
    a OR b is true, if at least one of a and b is true.
    NOT is negation, NOT true is false, NOT false is true.

  3. #3
    Registered User
    Join Date
    Jun 2005
    There's a bit more than that in C and C++

    Firstly, there is short circuit evaluation. An example of short circuit evaluation is when testing x && y. First x is evaluated. If x is false, y is not evaluated, because it not needed to get the final result (false AND anything is always false).

    Also, a zero value is false and any non-zero value is true. So "if (42) something();" will always execute something. This works for basic numeric types (bool, int, char, float, etc) as well as for pointers (a zero pointer is the NULL pointer, a special value that does not point at any object).

    The rules change a bit for overloaded operators in C++, but that's another story.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Would you guys be able to proved an example where this is used so I can play around with it? I tried to aply it to this someway and could not figure it out.

    #include <iostream>	
    using namespace std;
    int main()                            // Most important part of the program!
      int age;                            // Need a variable...
      cout<<"Please input your age: ";    // Asks for age
      cin>> age;                          // The input is put in age
      cin.ignore();                       // Throw away enter
      if ( age < 100 ) {                  // If the age is less than 100
         cout<<"You are pretty young!\n"; // Just to show you it works...
      else if ( age == 100 ) {            // I use else just to show an example 
         cout<<"You are old\n";           // Just to show you it works...
      else {
        cout<<"You are really old\n";     // Executed if no other statement is

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    You'd probably have to explain what's wrong with the examples you already have if you want other examples.

  6. #6
    Registered User
    Join Date
    Apr 2010
    +1 @tabstop

    To the original poster, it's unclear if you're having problems with C++ syntax or the boolean logic itself. To answer one of your side questions, boolean logic is VERY important in programming, so yes it is quite important that you become comfortable with it.

    These basic boolean operators accept arguments which can be thought of as statements or propositions (mathematical in nature). It is important that these statements passed as arguments have a clear TRUTH or FALSITY (these are called truth-values). Furthermore, each operator takes the propositions it's been given, and itself returns a truth-value.

    AND, OR take two arguments apiece.
    NOT takes only one proposition as an argument.

    The computer uses 0 to represent FALSITY and non-zero numbers to represent TRUTH.

    A AND B  , denoted A && B         returns TRUE if both A and B are true
    A OR B    , denoted A || B            returns TRUE if one or both of A, B are true 
    NOT A     , denoted   A  == 0        returns TRUE if A is a FALSITY
    //based on the info I've given you, we could denote NOT A differently, and in fact we often use 
    //another syntax to avoid typing out " == 0 " ... can you work out how?
    An IF statement works on the principle of IF->THEN statements from boolean algebra.

    if( some_expression ){
    // execute something desirable in here
    The IF statement will only execute the code in the curly brackets if some_expression evalutes to a non-zero number.

    Boolean logic may be studied independently of how computers represent it, and it might be easier for you to try some basic non-computer examples of logic puzzles before implementing it in a program. Such examples can be found by searching "logic" or "proposotional logic" or "propositional calculus" or "boolean" or "LSAT" or something like that on Google. Also, Google "DeMorgan's Law" afterwards... if you can wrap your head around that, you should be able to start using AND, OR, NOT, IF with some confidence.
    Last edited by Ocifer; 07-25-2011 at 11:29 AM.

  7. #7
    Registered User
    Join Date
    Jul 2011
    I can give that a try. From what I know I am understanding the c++ but, confused on the boolean logic. Just statred the other night learning this. After the first tutorial it jumped into this right away and was like wooo.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boolean operators
    By forkpie hat in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2008, 03:35 AM
  2. C++ and Boolean Operators
    By Rockskin in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2006, 03:45 PM
  3. Boolean operators
    By Trogdor27 in forum C++ Programming
    Replies: 10
    Last Post: 09-12-2005, 06:46 AM
  4. boolean operators
    By bj31t in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2004, 08:34 PM
  5. Boolean Operators?
    By civilwarsearch in forum C++ Programming
    Replies: 11
    Last Post: 12-15-2003, 09:50 PM