-
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.
-
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.
-
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.
-
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.
-
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.
-
It allows you to combine multiple truth values into one, which is necessary to formulate complex conditions.
-
-
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.
-
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.
-
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?
-
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
-
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.