New to C++ and confused by boolean and ifs and else

This is a discussion on New to C++ and confused by boolean and ifs and else within the C++ Programming forums, part of the General Programming Boards category; Hello I'm new To C++ and I have been reading the tutorials I have gotten to the if statment chapter ...

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

    New to C++ and confused by boolean and ifs and else

    Hello I'm new To C++ and I have been reading the tutorials I have gotten to the if statment chapter and I'm getting very confused by all the NOT OR ANDs. Is there any way someone could help me out? I have reread the booleans part aleast 5 times and I'm still confused by it. I if anyone could help me out that would be awsome. Thanks for your time!

  2. #2
    Registered User stuart_cpp's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    31
    Boolean value is the reult of a comparison or a logical occasion using AND or OR, which can be true of false. C++ used the bool type to represent boolean values. An expression of the type bool can either be true or false, where the internal value got true will be represented as the numerical value 1 and flase by a zero.

    Syntax:
    Code:
    bool true; // Or false...

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    If you have an expression 'a || b' you can reconsider it as "if either a or b is true (non-zero)" and if you have an expression 'a && b' then you can reconsider it as "if both a and b are true". When you get to the exercises on this site (which in no way reflect what you need to write in real-life code you will see things like this:

    if( (1 || 0) && (1 && 1) )

    You can reinterpret is as "if either 1 or 0 are true (non-zero) and if both 1 and 1 are true (non-zero) then execute some code". Since in the first expression, 1 is non-zero and in the second expression both 1's are non-zero, you get to 'if( 1 && 1 )' which basically means "if 1 and 1 are non-zero" which will be evaluated to true because both 1's are non-zero. I hope I didn't confuse you too much =]

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    AND, NOT, OR, XOR, are those same operators you learned in school. I suggested you switch back to those days where this was taught to you in math class. A good source is to search google for "math truth tables".

    When you have something like myInt > 12 you have a proposition. Basically this proposition states that "myInt is greater than 12".

    Now, propositions have a value. If I say "2 potatoes make a pair of potatoes", i've built a proposition and the value of this proposition is TRUE. Because indeed 2 potatoes make a pair of potatoes.

    So, assuming the variable myInt is equal to 17. The proposition above is also true. Because if myInt equals 17, then myInt certainly is greater than 12.

    You are now half-way there.

    Logical operators in C++ produce logical expressions. What they do is they grab one or more propositions and evaluate them producing one of two possible values. TRUE or FALSE

    Let's see...

    My car has wheels AND my car can fly. (propositions underlined, logical operator in bold)

    Without any math what is the value of that expression. you know that the second proposition is false. So, If I just said that to you your answer would be: "That's a lie!"

    The value of that expression is thus false. My car has wheels alright, but it certainly can't fly. The operator AND evaluated my expression to false. True AND False = False.

    But what if I had said:

    My car has wheels OR my car can fly. (propositions underlined, logical operator in bold)

    You answer would probably be "Your car has wheels". You couldn't say that my statement was false. It was true. True OR False = True.

    So, taking this to C++... Let's go back to the myInt example:

    Code:
    myInt = 17;
    myInt > 12 && myInt < 25;  // && means AND in C++
    myInt < 12 || myInt < 25;   // || means OR in C++
    What is the value of each of those expressions?
    The first is true. The second is true. In order to be able to evaluate you will have to remember about the truth tables. Again, search google for "math truth tables". They will tell you all combinations of true and false and the possible results for each of the logical operators.

    An if statement evaluates a logical expression. It is the equivalent of saying: IF myInt > 12 do this, ELSE do that.

    Code:
    if( myInt < 12 ) {
        // do this
    }
    else {
        // do that
    }
    And this is how boolean expressions are evaluated in C++ (if you don't mind the crude explanation).

    One last thing is left to be said.

    In C++ (as in most other languages) TRUE and FALSE have a value. False evaluates to 0, while True evaluates to everything else.

    What exactly does this mean? Well, any value that can be treated as a number can also be treated as a boolean (or logical value). And when that happens, when we want to treat that number has a boolean, any value other than 0 is considered true, while any value equal to 0 is considered false.

    So, with our example of myInt above, I could write:

    Code:
    if( myInt ) {
       // do something here if the variable myInt is not equal to 0
    }
    else {
       // do something here if the variable myInt is equal to 0
    }
    Last edited by Mario F.; 08-01-2006 at 07:59 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Now a test:
    It enters the loop or not?
    Code:
    // || is for OR
    // && is for AND
    // ^ is for XOR
    int test =23, k=0;
    while(((test-20)>(k+3)) ^ ((1 && 0) || true)) {...}
    Solve it without running the code.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    My answer is: no
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I'm getting very confused by all the NOT OR ANDs.
    This should get easier once you start working on a more realistic program where the variables actually mean something to you:

    Examples:
    IF there is a blank CD in the drive, AND IF the user clicks "burn"...
    IF there is NOT a blank CD in the drive, AND IF the user clicks "burn"...


    "IF", "AND", and "OR" have the same meanings in C++ as in English.

    We don't use "NOT" in exactly the same way, but it's similar.

    We don't say "EXCLUSIVE-OR", we say "One or the other, but not both."

    If you are going to become a good programmer, you will have to learn to use Boolean logic more abstractly. But, don't get too bogged-down with it right now.


    I have been reading the tutorials... I have reread the booleans part aleast 5 times and I'm still confused by it.
    With any technical subject, you are going to have to read the material more than once. (After 5 times, I guess it's time to ask a question or consult another reference..) Consider getting your hands on a beginning C++ book. A beginning book will cover about the same material as the tutorials, but in much more detail, and with much more explanation. For example, Accelerated C++ is about 350 pages, and Teach Yourself C++ in 21 Days is about 700 pages! (See the recommended book post at the top of the forum.)

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    >>My answer is: no
    Wrong answer.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Code:
    // || is for OR
    // && is for AND
    // ^ is for XOR
    int test =23, k=0;
    while(((test-20)>(k+3)) ^ ((1 && 0) || true)) {...}
    My answer is:
    So help me if you ever work on a project with me and give me code like that....you are off the project.

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    My answer is:
    So help me if you ever work on a project with me and give me code like that....you are off the project.
    Wrong answer,
    It is just a simple test not a real world code.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

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