C++ Boolean Type: Usage & Examples
In C++, bool is a fundamental data type that represents boolean values, true or false. This type is typically used for conditional statements and logical operations. Variables of type bool can only store either true or false values and are commonly used to determine if a condition is met or not.
Here is an example of how to use the bool data type:
bool isTrue = true;
bool isFalse = false;
if (isTrue) {
cout << "This is true." << endl;
} else {
cout << "This is false." << endl;
}
In this example, isTrue and isFalse are variables of type bool, storing true and false respectively. In the if statement, we check if isTrue is true. If it is, we output “This is true.” Otherwise, we output “This is false.”