Variable Type bool
-
The C++ reserved word to specify variables of the Boolean type is bool.
-
Boolean variables can only take on 2 possible values: true and false.
-
They can be declared and initialized as follows:
bool is_feeling_lucky = true;
Note that the reserved words true and false are not enclosed by single or double quotes.
-
In the input and output of Boolean variables, 1 is used to represent true,
and 0 for false.
-
Example: the code
bool is_stolen_money_spent;
cout << "Did you use up all the money that you stole from me?\n";
cin >> is_stolen_money_spent;
cout << "The answer is: "
<< is_stolen_money_spent << endl;
yields the following output:
Did you use up all the money that you stole from me?
1
The answer is: 1
-
Boolean variables and expressions are often used in making decisions and
in controlling the repetition of loops.