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.
Syntax:
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.
Important Points
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.
Example:
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:
C++
// C++ program to demonstrate
// bool data type
#include <iostream>
using namespace std;
// Driver code
int main()
{
int x1 = 10, x2 = 20, m = 2;
bool b1, b2;
// false
b1 = x1 == x2;
// true
b2 = x1 < x2;
cout << "Bool1 is = " <<
b1 << "\n";
cout << "Bool2 is = " <<
b2 << "\n";
bool b3 = true;
if (b3)
cout << "Yes" << "\n";
else
cout << "No" << "\n";
int x3 = false + 5 * m - b3;
cout << x3;
return 0;
}
OutputBool1 is = 0
Bool2 is = 1
Yes
9
Similar Reads
Boolean Data Type In programming languages, we have various data types to store different types of data. Some of the most used data types are integer, string, float, and boolean. The boolean data type is a type of data that stores only two types of values i.e. True or False. These values are not case-sensitive depend
13 min read
C/C++ Tricky Programs We may come across various tricky programs in our day-to-day life. Maybe in technical interviews, coding tests, or C/C++ classrooms. Here is a list of such programs:- Print text within double quotes (" "). This may seem easy, but beginners may get puzzled while printing text within double quotes. C
6 min read
C++ Logical Operators In C++ programming languages, logical operators are symbols that allow you to combine or modify conditions to make logical evaluations. They are used to perform logical operations on boolean values (true or false).In C++, there are three logical operators:Table of ContentLogical AND Operator ( &
3 min read
bitset any() in C++ STL The bitset::any() is an inbuilt function in C++ STL which returns True if at least one bit is set in a number. It returns False if all the bits are not set or if the number is zero. Syntax: bool any() Parameter: The function does not accepts any parameter. Return Value: The function returns a boolea
2 min read
Comparison of boolean Data Type in C++ and Java The Boolean data type is one of the primitive data types in both C++ and Java. Although it may seem to be the easiest of all the data types, as it can have only two values, true or false. The main difference between them is listed below:In Java, the boolean type can hold false or true.In C++, the bo
4 min read