The next data type we’re going to look at is the boolean data type. Boolean variables only have two possible values: true (1) and false (0).
To declare a boolean variable, we use the keyword bool.
bool bValue;
When assigning values to boolean variables, we use the keywords true and false.
bool bValue1 = true; // explicit assignment bool bValue2(false); // implicit assignment
Just as the unary minus operator (-) can be used to make an integer negative, the logical NOT operator (!) can be used to flip a boolean value from true to false, or false to true:
bool bValue1 = !true; // bValue1 will have the value false bool bValue2(!false); // bValue2 will have the value true
When boolean values are evaluated, they actually don’t evaluate to true or false. They evaluate to the numbers 0 (false) or 1 (true). Consequently, when we print their values with cout, it prints 0 for false, and 1 for true:
bool bValue = true; cout << bValue << endl; cout << !bValue << endl; bool bValue2 = false; cout << bValue2 << endl; cout << !bValue2 << endl;
Outputs:
1 0 0 1
One of the most common uses for boolean variables is inside if statements:
bool bValue = true;
if (bValue)
cout << "bValue was true" << endl;
else
cout << "bValue was false" << endl;
Output:
bValue was true
Don’t forget that you can use the logical not operator to reverse a boolean value:
bool bValue = true;
if (!bValue)
cout << "The if statement was true" << endl;
else
cout << "The if statement was false" << endl;
Output:
The if statement was false
Boolean values are also useful as the return values for functions that check whether something is true or not. Such functions are typically named starting with the word Is:
#include <iostream>
// returns true if x and y are equal
bool IsEqual(int x, int y)
{
return (x == y); // use equality operator to test if equal
}
int main()
{
cout << "Enter a value: ";
int x;
cin >> x;
cout << "Enter another value: ";
int y;
cin >> y;
bool bEqual = IsEqual(x, y);
if (bEqual)
cout << x << " and " << y << " are equal"<<endl;
else
cout << x << " and " << y << " are not equal"<<endl;
return 0;
}
In this case, because we only use bEqual in one place, there’s really no need to assign it to a variable. We could do this instead:
if (IsEqual(x, y))
cout << x << " and " << y << " are equal"<<endl;
else
cout << x << " and " << y << " are not equal"<<endl;
IsEqual() evalues to true or false, and the if statement then branches based on this value.
Boolean variables are quite refreshing in their simplicity!
2.7 — Chars
|
Index
|
2.5 — Floating point numbers
|
2.7 — Chars
Index
2.5 — Floating point numbers
Correct me if I am wrong .Shouldnt function name be “isEqual” instead of “IsEqual”? or that convention mostly applies to variable names.
My personal preference is that variable names start with lower case and function (and class) names start with upper case. This makes them easier to distinguish.
I’m confused as to why you make the IsEqual function a bool, wouldn’t making it an int function work just as well? Or is it declared as a bool function so you have an idea of what the return values will be and what the function does? Sorry if I sound like a newbie :p
IsEqual function isn’t a bool, it returns a bool. Boolean values are used to represent values that can only be true or false. When we’re talking about whether a value is equal, either it is (true), or it isn’t (false). Consequently, it makes more sense to return a bool than an int. While technically returning an int would work (returning the value 1 if the parameters are equal, and 0 otherwise), returning a bool is more intuitive and leaves less room for errors to be made.
I have problem actually I don’t know how to use the boolean with ( cin ) ..
like this example:
if i want to ask some one about his car ?
===========
bool car;
=====
is the above good solution or not ??
…
i’m witing for your answer
thank you so much
When trying to cin boolean values, if the user enters the number 0, this will be treated as the value false. If the user enters anything else, it will be treated as the value true.
There are several ways to proceed:
* Tell the user to enter 0 for no and 1 for yes and then cin a bool.
* Tell the user to enter ‘y’ or ‘n’ and then cin a character.
Once you get to the lesson on strings, you could also do this:
* Tell the user to enter “yes” or “no” and then cin a string.
[...] 2007 Prev/Next Posts « 2.6 — Boolean Values | Home | 2.8 — Constants » Saturday, June 9th, 2007 at 7:07 [...]