Table of Contents
The bool data type represents logical values. It can store only one of two possible values: true or false. Boolean values are commonly used to represent the result of comparisons and control program flow.
This example demonstrates how to declare and display a Boolean variable.
Source Code #
#include <iostream>
int main()
{
bool is_logged_in = true;
std::cout << "User Logged In: " << is_logged_in << '\n';
return 0;
}
Output #
User Logged In: 1
Explanation #
bool is_logged_in = true;declares a Boolean variable and initializes it with the valuetrue.- A
boolobject can store onlytrueorfalse. - By default,
std::coutprints Boolean values as integers, wheretrueis displayed as1andfalseas0. - The manipulators
std::boolalphaandstd::noboolalphacan be used to display Boolean values as the wordstrueandfalseinstead of1and0. std::cout << "User Logged In: " << is_logged_in << '\n';prints the value stored in the Boolean variable.return 0;terminates the program successfully.