Thread: Boolean Operators...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Question Boolean Operators...

    Hey everyone, I'm just beginning to learn C++ and its just about the first programming language I've ever learned (Aside from VB6) and I have to say its quite intimidating, so I have a few questions about boolean operators, after reading tutorial 2 and was wondering if any of you could elaborate on them and give me a down to earth explanation.

    How do boolean operators work? Are they constantly comparing to other numbers? I'm sure if someone could possibly post a little bit of code with these operators in work, I could figure it out, I'm just confused on how to actually use them... If anyone could elaborate, I'd deeply appreciate it.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The boolean operators are and, or, and not. They mean "and", "or", and "not" in roughly that order. (I.e., "not" means the opposite; "and" means this "and" that, etc.)

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Boolean operator are just operators that work on bools. They also work on integers and pointers, where 0 and NULL work like false, and all other values work like true.

    The boolean operators are && (AND), ||(OR) and !(NOT). You can also use != and == with bools, which function like XOR and NXOR.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Boolean operators work on anything that are boolean expressions. Boolean expressions is anything that can be expressed as true or false. That means they are not just limited to integers.
    Example: MyString == "Hello World" is a boolean expression.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Where T means "true" and F means "false"

    AND (&):
    Both sides of the expression must be true to produce a "true" result.
    (Both must = T)
    T & T = T
    T & F = F
    F & T = F
    F & F = F

    OR (|):
    Either side of the expression can be true to produce a "true" result.
    (Both, or one must = T)
    T | T = T
    T | F = T
    F | T = T
    F | F = F

    NOT (!):
    The opposite.
    !T = F
    !F = T
    goto( comeFrom() );

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also, it should be easy to remember that they work just as in "real life".
    If this and that is true, then the result must also be true, no?
    If you have a negative sentence and then apply a word to negate it, then it would turn positive.

    Example: I do not doubt this.
    Doubt is usually negative by default, but not turns it positive.

    Example: I want this OR that. So if someone gives you that or this (or both), then you accept it.
    Example: You want this AND that. So if someone gives you just one, then you do not want it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    a slight aside..if we use bool data types should assignment be done using 'true' / 'false' or is it ok to use 1 / 0 /null, is this immutable anyway and just best to keep to same throughout for clarity?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can assign anything that is a boolean expression, ie something that can be represented with true or false.
    0 and 1 are boolean expressions so they work fine. But there is no /null.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rogster001
    if we use bool data types should assignment be done using 'true' / 'false' or is it ok to use 1 / 0 /null, is this immutable anyway and just best to keep to same throughout for clarity?
    If you want to explicitly assign a boolean value to a bool variable then use true or false. It is certainly okay to use 1 or 0 instead, but it does not take advantage of what the introduction of bool in C++ offers. null is neither a keyword nor an identifier from the standard library, but you are probably talking about NULL from <cstddef>, which is typically just 0 (and definitely is equal to 0).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  3. C++ and Boolean Operators
    By Rockskin in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2006, 03:45 PM
  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