Skip to main content
Engineering LibreTexts

6.3: Boolean Data Type

  • Page ID
    29059
  • Discussion

    The Boolean data type is also known as the logical data type and represents the concepts of true and false. The name "Boolean" comes from the mathematician George Boole; who in 1854 published: An Investigation of the Laws of Thought. Boolean algebra is the area of mathematics that deals with the logical representation of true and false using the numbers 0 and 1. The importance of the Boolean data type within programming is that it is used to control programming structures (if then else, while loops, etc.) that allow us to implement "choice" into our algorithms.

    The Boolean data type has the same attributes and acts or behaves similarly in all programming languages. The rules within the C++ programming language are:

    C++ Reserved Word bool
    Represent Logical concepts of true and false
    Size Usually 1 byte
    Normal Signage Unsigned
    Domain (values allowed) 0 meaning false, and 1 meaning true
    C++ syntax rule true and false are reserved words that can be used as values in expressions
    C++ concept/rule Any value from any data type can be demoted into a Boolean data type with zero representing false and all non-zero values representing true

    Bool data type in C++

    The C++ Standard has added the bool data types to the C++ specifications.They are provided to provide better control in certain situations as well as for providing conveniences to C++ programmers.

    As mentioned above, bool values actually evaluate to either 0, which is FALSE, or 1, which is TRUE.

    bool b1 = true;      // declaring a boolean variable with true value   

    In C++, the data type bool has been introduced to hold a boolean value, true or false.The values true or false have been added as keywords in the C++ language.
    Important Points:

    • The default numeric value of true is 1 and false is 0.
    • We can use bool type variables or values true and false in mathematical expressions also.For instance,
      int x = false + true + 6;

      is valid and the expression on right will evaluate to 7 as false has value 0 and true will have value 1.

    • It is also possible to convert implicitly the data type integers or floating point values to bool type
      bool x = 0; // false 
      bool y = 100; // true 
      bool z = 15.75; // true

     

    Adapted from: 
    "Boolean Data Type" by Kenneth Busbee, Download for free at http://cnx.org/contents/303800f3-07f...93e8948c5@22.2 is licensed under CC BY 4.0
    "Bool data type in C++" by Mrigendra Singh, Geeks for Geeks is licensed under CC BY-SA 4.0