boolean

This is a discussion on boolean within the C++ Programming forums, part of the General Programming Boards category; Hi! I will say sorry if the question has been asked or posted. But I am a newbie. What confuses ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    65

    boolean

    Hi! I will say sorry if the question has been asked or posted. But I am a newbie. What confuses me is the boolean thing that I really don't understand. I am just wondering what it is used for? Is it only used for true and false? Does the expression always have to be like Not, Braces, and then OR and AND? And does the number always have to be 0 // 1 or 1 // 0 in comparison? I am just quite confused when it comes to boolean in the tutorial. If somebody is willing or cares to explain in details, I would be really grateful to that.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    I am just wondering what it is used for?
    Any time you need to store the answer to a yes or no questions. In other words, very often.

    Is it only used for true and false?
    Yes.

    Does the expression always have to be like Not, Braces, and then OR and AND?
    No. Any non zero or non null value when used in a boolean context, such as assigning to a boolean variable, will evaluate to true. 0 and null will evaluate to false.

    And does the number always have to be 0 // 1 or 1 // 0 in comparison?
    Typically you compare with keywords true and false. But it you cast a boolean to an numeric type, explicitly or implicitly, then it will be either 1 or 0.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Is it possible for you to give me an example or text or something and explain it like a tutorial. I know it would seem to be pestering but I would really wanna get the heck of it. I really appreciate that.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Are you talking about boolean logic (like (1 && (1 || 0)) type stuff)? That is more complicated than a simple bool variable and commonly confuses people. If that is your problem, you might also want to try searching this forum as it has been asked about many times, especially in reference to the main site's tutorial, and there have been several good threads with detailed answers.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Yes it is. That's what I have been asking. Besides, what does it do? I would like to see an example of the structure with clear explanation that shows you step by step. Even some books can't really clarify this.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It allows you to combine multiple truth values into one, which is necessary to formulate complex conditions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not sure which part you said yes to, but Boolean Algebra is described in a few Wiki articles:
    http://en.wikipedia.org/wiki/Boolean..._(introduction)
    http://en.wikipedia.org/wiki/Boolean_algebra_(structure)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Don't get too bogged-down in the abstract...

    In real-world programs, the variables represent the state of something "real", like : IF (a blank CD is inserted) AND (the user clicks "burn")...

    Bitwise manipulation is similar, in that each bit usually represents the state of something. (There are exceptions, such as XOR encryption.)

    Most of the time, you won't see the words "true" or "false", or the numbers "1" or "0".

    This is how computer programs "make decisions" with if-statements. Every decision/choice only has only two outcomes... True/False (yes/no).

    In many real situations, there is more than one possibility. Typically, you have to continue through a series of true/false questions/decisions 'till you find a true result, or until you find the right combination of true results.


    Once you start to understand how Boolean logic is used in "real programs"... perhaps after you've written a couple of programs or studied a few examples... you should go-back and make sure you understand the abstract concepts & notation.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    That's such a hard subject. Even I tried to learn the boolean in Wiki. I still have a hard time understanding the logic and the intro, not to mention about the structure. What I know is that it only has true and false function but how it is applied to real world or real situation and it is used in a single piece of program is what I really wanna see in order to understand what it really is and how it really works. Since most of the example could be found regarding boolean structure but they do not tell you why in this area you have to use boolean and how it works. The tutorial here does not really state clearly how it works and it doesn't have any example, it just tells you what it is. I really wanna learn it. Without a good understanding of it, video game design would be doomed.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include <iostream>
    
    int main()
    {
    	int answer; /* This will hold the user's answer */
    
    	cout << "Enter a number less than 50, but more than 20: " << endl ;
    
    	cin >> answer ;
    
    	if ( answer < 50 && answer > 20 )
    		cout << "You followed the directions" << endl ;
    	else
    		cout << "You didn't follow the directions" << endl ;
    
    
    	return 0;
    }
    Does that help?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Computers in general see things are either true or false. Everything is.

    The common logical operators are:
    OR - if one OR the other is true.
    AND - if BOTH are true.
    NOT - it's true when it's NOT true.

    In computers, we use this to solve problems where something has to be combined with something else:

    Code:
    if (input > 0 && input < 5) 
       ... do something  
    else
       ... out of range
    The if-statement is true when input is greater than 0 AND inptu is less than 5 (so the valid range is 1..4 if input is an integer).
    or
    Code:
    if (a == 7 || (b == 8 && c == 9))
    {
       ... do some stuff
    }
    True if either a is 7, or if both (b is 8 and c is 9). It will also (see below) be true if a is 7 and b is 9 and c is 9 - since EITHER side can be true to give a true result.

    Note that OR in computers is "inclusive", not like humans where we see OR mostly as an exclusve combination: Are you married or single, do you want coffee or tea. Those can not be true at the same time (unless you are lying or like strange hot drinks).

    There is a logical operator called "XOR" which is "exclusive or" - it is only available as a binary operator in C and C++ (using the ^ symbol). It is truly a OR in human ways: If one side is true and the other is false, the result is true. But we don't use that in C or C++ boolean logic.

    Computer AND is much more like our "human AND" - both sides need to be true for the result to be true.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Thanks so much and it is much more clear now. I think I get the heck of it sort of.

    Since I am a newbie, I still have a lot of questions to ask and a long way to learn. The thing is I am not sure whether you guys wanna help me or not. Something that confuses me is the while loop and do while loop as well as function. I will be making a new thread regarding this subject.
    Last edited by kenryuakuma; 09-08-2008 at 11:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice needs help
    By ghaasemi in forum C++ Programming
    Replies: 9
    Last Post: 05-30-2009, 08:20 AM
  2. Casting boolean as string
    By doofusboy in forum C Programming
    Replies: 11
    Last Post: 11-10-2005, 11:24 AM
  3. Replies: 4
    Last Post: 04-02-2004, 06:30 PM
  4. Working with boolean...
    By CompiledMonkey in forum C Programming
    Replies: 4
    Last Post: 11-03-2003, 09:39 AM
  5. Use struct to create a boolean type
    By skyglin in forum C Programming
    Replies: 6
    Last Post: 06-18-2003, 08:21 PM

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