Thread: NOT, AND, OR- I do not understand at all.

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

    NOT, AND, OR- I do not understand at all.

    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.


    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)

    Why does not false evaluate to true and vice versa???

    What the heck do those questions mean??

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Any value besides 0 is considered true. Only 0 is false. The ! operator simply evaluates to the opposite of whatever the value originally was. !0 evaluates to 1 (true) because 0 is false, !437 evaluates to 0 (false) because 437 is true.

    What do you mean what do the questions mean? Would you like someone to just retype what you posted?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    4
    The part:


    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)

    I dont understand how they get an answer.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Phanntom
    Why does not false evaluate to true and vice versa???
    You have to be a little more specific. False means false. Why do you think it would evaluate to true? A condition is either true or false. It can't be anything else (it can in fact be undefined. But that's beyond the meaning of that article). The only way for true to evaluate to false is negating it.

    > What the heck do those questions mean??

    They are to be viewed as just an helper as to understand the operators and how to use them. In real code you will be writing things differently. For instance,
    Code:
    int value = 12;
    int another_value = 24;
    
    if(value < another_value && value > 6) {
        std::cout << "value is smaller than another_value AND it is bigger than 6"
    }
    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
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You are supposed to evaluate each expression. Start on the inside and evaluate one part of it. For example, in question A you can evaluate 1 || 0. What does 1 || 0 evaluate to? You have to first understand why it evaluates to 1 before moving on. If you don't, re-read the section on OR.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    4
    but isnt not solved first

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Phanntom
    but isnt not solved first
    Parentheses are used to change the normal order of operations. For boolean expressions, always evaluate the sets of parentheses inside-out. Since the ! is outside the last set of parentheses, it's evaluated last.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    4
    thanks that was what was confusing me thanks a lot

Popular pages Recent additions subscribe to a feed