Variable Type bool

  1. The C++ reserved word to specify variables of the Boolean type is bool.

  2. Boolean variables can only take on 2 possible values: true and false.

  3. They can be declared and initialized as follows:
    bool is_feeling_lucky = true;
    Note that the reserved words true and false are not enclosed by single or double quotes.

  4. In the input and output of Boolean variables, 1 is used to represent true, and 0 for false.

  5. Example: the code

    bool is_stolen_money_spent;
    cout << "Did you use up all the money that you stole from me?\n";
    cin >> is_stolen_money_spent;
    cout << "The answer is: "
             << is_stolen_money_spent << endl;

    yields the following output:

    Did you use up all the money that you stole from me?
    1
    The answer is: 1

  6. Boolean variables and expressions are often used in making decisions and in controlling the repetition of loops.