Bolean Operators hurt my head. (Trouble understanding) :(

This is a discussion on Bolean Operators hurt my head. (Trouble understanding) :( within the C++ Programming forums, part of the General Programming Boards category; Ok, well recently I took a long break from learning C++. I never did get far,basically understanding pointers, although I ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    Unhappy Bolean Operators hurt my head. (Trouble understanding) :(

    Ok, well recently I took a long break from learning C++. I never did get far,basically understanding pointers, although I did make a pretty useful application, simple but nice.

    Anyways, I am learning from step one, and I hit a wall early on. See, I never understood Boolean Operators.

    I will copy and paste the main section so you guys don't have to scrounge around.

    Boolean operators allow you to create more complex conditional statements. For example, if you wish to check if a variable is both greater than five and less than ten, you could use the boolean AND to ensure both var > 5 and var < 10 are true. In the following discussion of boolean operators, I will capitalize the boolean operators in order to distinguish them from normal english. The actual C++ operators of equivalent function will be described further into the tutorial - the C++ symbols are not: OR, AND, NOT, although they are of equivalent function.

    When using if statements, you will often wish to check multiple different conditions. You must understand the Boolean operators OR, NOT, and AND. The boolean operators function in a similar way to the comparison operators: each returns 0 if evaluates to FALSE or 1 if it evaluates to TRUE.

    NOT: The NOT operator accepts one input. If that input is TRUE, it returns FALSE, and if that input is FALSE, it returns TRUE. For example, NOT (1) evalutes to 0, and NOT (0) evalutes to 1. NOT (any number but zero) evaluates to 0. In C and C++ NOT is written as !. NOT is evaluated prior to both AND and OR.

    AND: This is another important command. AND returns TRUE if both inputs are TRUE (if 'this' AND 'that' are true). (1) AND (0) would evaluate to zero because one of the inputs is false (both must be TRUE for it to evaluate to TRUE). (1) AND (1) evaluates to 1. (any number but 0) AND (0) evaluates to 0. The AND operator is written && in C++. Do not be confused by thinking it checks equality between numbers: it does not. Keep in mind that the AND operator is evaluated before the OR operator.

    OR: Very useful is the OR statement! If either (or both) of the two values it checks are TRUE then it returns TRUE. For example, (1) OR (0) evaluates to 1. (0) OR (0) evaluates to 0. The OR is written as || in C++. Those are the pipe characters. On your keyboard, they may look like a stretched colon. On my computer the pipe shares its key with \. Keep in mind that OR will be evaluated after AND.

    It is possible to combine several boolean operators in a single statement; often you will find doing so to be of great value when creating complex expressions for if statements. What is !(1 && 0)? Of course, it would be TRUE. It is true is because 1 && 0 evaluates to 0 and !0 evaluates to TRUE (ie, 1).

    Try some of these - they're not too hard. If you have questions about them, feel free to stop by our forums.

    Code:
    A. !( 1 || 0 )         ANSWER: 0	
    B. !( 1 || 1 && 0 )    ANSWER: 0 (AND is evaluated before OR)
    C. !( ( 1 || 0 ) && 0 )  ANSWER: 1 (Parenthesis are useful)
    None of it just makes sense (Mainly the code part). I was wondering if anyone here could explain it better, or even make a simple program and explain it to me.

    I am sorry if my confusion wasn't descriptive enough, but this is just one of those things I totally do not understand. Thanks in advanced for any help provided.

    (Also note, I try to keep my grammar and spelling as neat as possible, sorry for any mistakes.)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you know what the words "and", "or", and "not" mean in English, well, then that's it. (Almost: in English, sometimes "or" means "one, or the other, but not both" -- but never in C++.)

    The only other thing to remember is 0 = FALSE and number-that-isn't-0 = TRUE. (The output of the logical operators for TRUE will always be 1.)

    I never remember the order of operations either, so I always use parentheses. There's probably a list of order of operations in C++ on the web somewhere.

    Edit to add: I suppose I could give examples. Say I want to print out all the prime divisors of a number. So, I should print n if it is prime and it is a divisor of d:
    Code:
    if (isPrime(n) && isDivisorOf(n,d))
    Say I'm writing a turn-based game; I need to keep going until somebody wins:
    Code:
    while (!(PlayerWon || ComputerWon || Tie))
    If the player won or the computer won or it's a tie, I want to stop, so my while loop needs to keep going in the opposite case -- hence the not.
    Last edited by tabstop; 01-20-2008 at 06:01 PM.

  3. #3
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    since we all speak English.
    Think of '&&' as literal 'and', so you are looking for both to be true
    And think of '||' as literal 'or'. so you are looking for either one to be true.
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Quote Originally Posted by tabstop View Post
    If you know what the words "and", "or", and "not" mean in English, well, then that's it. (Almost: in English, sometimes "or" means "one, or the other, but not both" -- but never in C++.)

    The only other thing to remember is 0 = FALSE and number-that-isn't-0 = TRUE. (The output of the logical operators for TRUE will always be 1.)

    I never remember the order of operations either, so I always use parentheses. There's probably a list of order of operations in C++ on the web somewhere.

    Edit to add: I suppose I could give examples. Say I want to print out all the prime divisors of a number. So, I should print n if it is prime and it is a divisor of d:
    Code:
    if (isPrime(n) && isDivisorOf(n,d))
    Say I'm writing a turn-based game; I need to keep going until somebody wins:
    Code:
    while (!(PlayerWon || ComputerWon || Tie))
    If the player won or the computer won or it's a tie, I want to stop, so my while loop needs to keep going in the opposite case -- hence the not.
    Ahh, I kind of see what you are saying. I guess the examples they supplied just did not make sense to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble understanding collision detection code
    By silk.odyssey in forum Game Programming
    Replies: 5
    Last Post: 06-16-2004, 02:27 PM
  2. Having Trouble Understanding Classes
    By prog-bman in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2004, 12:44 PM
  3. a little trouble understanding
    By zema in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2003, 04:49 PM
  4. having trouble understanding this statement
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2003, 09:00 AM
  5. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 01:23 PM

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