• Home
  • 1.16 Boolean Data Type

1.16 Boolean Data Type

View Categories

1.16 Boolean Data Type

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 value true.
  • A bool object can store only true or false.
  • By default, std::cout prints Boolean values as integers, where true is displayed as 1 and false as 0.
  • The manipulators std::boolalpha and std::noboolalpha can be used to display Boolean values as the words true and false instead of 1 and 0.
  • std::cout << "User Logged In: " << is_logged_in << '\n'; prints the value stored in the Boolean variable.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *