In C++, boolean values are used to store true or false conditions. They help programmers make decisions and control program flow.
Declaring a Boolean Variable
To define a boolean variable, use the bool
keyword. A boolean can only hold two possible values: true
or false
.
Example
C++
x
using namespace std;
int main() {
bool isMotorcycleFast = true;
bool isHelmetOptional = false;
cout << isMotorcycleFast; // Outputs 1 (true)
cout << isHelmetOptional; // Outputs 0 (false)
return 0;
}
Boolean Values in C++
true
is internally represented as1
.false
is internally represented as0
.
This means boolean values can also be used in logical operations and conditional statements.