The ISO/ANSI C++ Standard has added certain new data types to the original C++ specifications. They are provided to provide better control in certain situations as well as for providing conveniences to C++ programmers. A boolean data type is declared with the bool keyword and can only take the values in either true or false form. One of the new data types is bool.
bool b1 = true; // declaring a boolean variable with true value
In C++, as mentioned earlier the data type bool has been introduced to hold a boolean value, true or false. The values true or false have been added as keywords in the C++ language.
1. The default numeric value of true is 1 and false is 0.
2. We can use bool-type variables or values true and false in mathematical expressions also. For instance,
int x = false + true + 6;
3. is valid and the expression on the right will evaluate to 7 as false has a value of 0 and true will have a value of 1.
4. It is also possible to convert implicitly the data type integers or floating point values to bool type.
bool x = 0; // false
bool y = 100; // true
bool z = 15.75; // true
The most common use of the bool datatype is for conditional statements. We can compare conditions with a boolean, and also return them telling if they are true or false.
Below is the C++ program to demonstrate bool data type:
Bool1 is = 0 Bool2 is = 1 Yes 9
This article is contributed by Mrigendra Singh.
Please Login to comment...