• In C++ Boolean data type is defined using the bool keyword.
  • This servers as an easy and convenient datatype for programmers to manage and write conditional statements using a boolean value, rather than an int.
  • Usually true or false are assigned to boolean variables as their default numerical values.
  • In C++ any numerical value can be assigned to a boolean variable, all values other than 00 are considered to be true and stored as 1, while 0 is considered to be false.
  • In certain situations, they are provided to provide better control as well as for providing conveniences to C++ programmers.
  • At C++ language the values true or false have been added as keywords.
  • We can use bool type values or variables true and false in mathematical expressions.

  • In numerical expression a boolean variable in C++ can be used as well.
  • if a bool variable is equal to true (or any numeric value other than 0), 1is assigned to it and taken as 1 during the evaluation of the expression; 0 and false will be taken as 0.

Sample Code

#include <iostream>
using namespace std;

int main() {
bool x = 10; // x = 1;
bool y = false; // y = 0;

// Using bool in a numeric expression:
cout << 2 * (x + y) << endl;

return 0;
}

Output

Categorized in: