Thread: New, and need Boolean help!

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    Lightbulb New, and need Boolean help!

    I just started programming and can someone explain the Booleans to me? I know they're the OR,AND, and NOT.
    I also know that the NOT operator accepts one input. the AND returns TRUE if both inputs are TRUE and the OR is if either (or both) of the two values it checks are TRUE than it returns TRUE or something like that but the problem is that I don't understand it real well and need some help with this because the site's tutorial isn't helpful enough (neither is the FAQ) I tried searching but all the people seem to know Booleans so all they ask is how to use improved booleans, But someone please explain the BOOLEANS to me.
    Too Fast Too Furious,
    too Fast For Y'all.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    p !p
    T  F
    F  T
    
    p q p&&q
    T T   T
    T F   F
    F T   F
    F F   F
    
    p q p||q
    T T   T
    T F   T
    F T   T
    F F   F
    There are the truth tables for the boolean operators. Really, just think about it logically. I oftentimes just think about an informal sentence; then, I convert that to a wff (well formed formula) or code.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    But my problem..

    But my problem is that I'm not sure how to use the codes and when they are used and why because I am just new to this stuff.
    Too Fast Too Furious,
    too Fast For Y'all.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    38

    Boolean examples.

    You would use booleans to check for true or false, or possibly more than one answer that needs to be checked for a condition.
    For example, checking if a user input is 'a' or 'A' when entering data.

    OR example:

    cin>>userInput;
    if (userInput == 'a' || userInput == 'A')
    cout<<"User has input an A";

    AND example:

    This usually checks two data entries or data values;
    Something like: type and quantity?

    cin>>type;
    cin>>quantity;
    if (type == 'h' && quantity == 1)
    cout<<quantity<<"hamburger is the amount I want";

    I hope this might give a little bit of an insight to what Boolean conditionals might be used for. This is just a very small example, so check with some C++ tutorials or a book for more in-depth reasoning.
    SilasP

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    Where can I?

    Where can I get a good tutorial with examples other than a book or this site's tutorials?
    Too Fast Too Furious,
    too Fast For Y'all.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    38

    Try this.

    There are some tutorials at the following site. Go to the tutorials main page and scroll down and click on the beginner tutorials. I don't know how good they are, but they may help. There are a lot of them there, so you might be able to find something that fits your C++ level.
    http://www.cpp-home.com

    ~SilasP
    SilasP

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Here's another tutorial.
    I'm not sure how to use the codes and when they are used and why
    Have you learned if-statements and switch-statements yet? These operators are used with decision making logic. I was working on a program a few days ago where I needed to break-out of of a test loop if TestCount > 20 AND if ErrorFlag was FALSE.

    When you need boolian logic... you'll know it! You'll use AND and OR, when there is more than one condition to test. There is often more than one way to work-out the logic. In the above example, I could have said keep looping if TestCount <= 20, OR ErrorFlag is TRUE.

    Sometimes I use NOT when I want to provide a default response.... I might ask the user to enter "C" OR "Q" for Continue or Quit. If the user enters something that is NOT Q OR q, the program contunues.

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    huhhm, thanks I think I understand what you mean.
    but this is what it say's in the tutorial:

    NOT: the NOT operator accepts one input; if that input is TRUE, it returns FALSE, and if that input is FALSE, it returns TRUE.


    AND: this is another important command; AND returns TRUE if both inputs are TRUE(if 'this'AND 'this' are true)


    OR: very useful is the OR statement! If either(or both) of the two values it checks are TRUE then it reurns TRUE.


    I think I understand but I need to be sure so can someone please give me a couple examples for each on(Please bare with me cuz I must be a pain in the a$$)
    Too Fast Too Furious,
    too Fast For Y'all.

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    38

    Simplest way I know.

    I posted some pretty simple code for OR,AND statements already. So I'm thinking you aren't getting the basic thought process of each.

    && means the conditional AND in code.
    || means the conditional OR in code.

    If you want to check if a boat is being rowed in a straight line; meaning both the left and the right oar must be in use or rowing. You would use an AND statement. Also, if that isn't the right answer, you could check to see, using an OR statement; if it is being rowed in a circle (Left or Right being true). The first IF statement will rule out the straight line, so this will proceed to checking for a circle path or no path. Possibly in the way of:

    if (leftRow && rightRow)
    cout<<"Straight path"; (This will print only if both are true)
    else if (leftRow || rightRow)
    cout<<"Going in a circle"; (This will print if only one is true)
    else
    cout<<"Going NOWHERE"; (This will print if neither is true)

    This is pretty much as basic as it gets.

    ~SilasP
    SilasP

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    oh, I get it now thanks a lot I just didn't seem to understand for some reason anyway thanks!
    Too Fast Too Furious,
    too Fast For Y'all.

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    38

    Good deal.

    Glad I could help. I've gotten a lot of help on this message board myself, so it felt good to be able to return the favor to someone who needed help.
    ~Silas P
    SilasP

Popular pages Recent additions subscribe to a feed