In this article, we’ll take a look at the Bool datatype in C++.
The bool data type is one that is there in C++ for quite sometime. This serves as a convenient and easy datatype for programmers to manage and write conditional statements using a boolean value, rather than an int.
However, remember that essentially, both bool
and int
are the same, with some small differences.
Let’s take a look at using this datatype, using a few examples!
The bool Datatype in C++
We can assign a variable of this datatype to only two values:
- true -> Equivalent to the integer value 1
- false -> Equivalent to the integer value 0
The following way shows how the variables are assigned.
bool var_1 = true; // Set var_1 to true
bool var_2 = false; // Set var_2 to false
Interestingly, if you try to print these variables using std::cout
, you’ll only get their integer values.
#include <iostream>
int main() {
bool var_1 = true;
bool var_2 = false;
std::cout << "var_1: " << var_1 << std::endl;
std::cout << "var_2: " << var_2 << std::endl;
return 0;
}
Output
var_1: 1
var_2: 0
Using bool data type in C++
The most common use of the bool
datatype is for conditional statements. We can compare conditions with a boolean, and also return them.
The below snippet shows the comparison of a condition with a bool
value.
#include <iostream>
int main() {
int x = 10, y = 20;
if ((x > y) == true)
// The condition evaluates to true
std::cout << "x > y\n";
else
// The condition evaluates to false
std::cout << "x <= y\n";
return 0;
}
Output
x <= y
We can also return conditional statements since they are also a bool
. We can use this to directly return them in functions
For example, if you declare a boolean function to check if the first argument is greater than the second, you can directly return a conditional statement (x > y).
bool check_greater(int x, int y) {
return (x > y);
}
This will return true
if x > y, and false
otherwise.
Conversion to int/float/double
Since the default value of true is 1 (or 1.00), we can also convert them into an int
/ float
/ double
by type casting.
Sometimes, even the compiler implicitly converts a bool
to an int
/ float
, and vice-versa. So this is very convenient now!
The below expression is valid in C++:
int x = 10;
float y = 2.5;
float z = x + y + true + false;
Here, since all the types can be converted to the left-hand side type float
, z
will be assigned a value of: 10 + 2.5 + 1 + 0 = 13.5
We can also do the reverse if the datatype of the LHS is bool
.
int x = 10;
float y = 2.5;
bool z = x + y + true + false;
Here, z
will be assigned true
, since (bool)10 + (bool)2.5 + true + false = true + true + true + false
= true
!
Using bool as a template argument
Since bool
is a valid data type, we can pass it as a template argument too.
For example, we can pass it as an argument to std::vector<T>
, to construct a vector of bool
s!
#include <iostream>
#include <vector>
int main() {
std::vector<bool> vec = {true, false, true, false};
for (auto i: vec)
std::cout << i << std::endl;
return 0;
}
Output
1
0
1
0
Conclusion
In this article, we learned about the bool data type in C++, and how we can use it in different ways, to make our lives easier!
References
- cppreference.com section on Bool data type