What is bool data in C++ ?

  • 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

[pastacode lang=”cpp” manual=”%23include%20%3Ciostream%3E%0Ausing%20namespace%20std%3B%0A%0Aint%20main()%20%7B%0A%20%20bool%20x%20%3D%2010%3B%20%20%2F%2F%20x%20%3D%201%3B%0A%20%20bool%20y%20%3D%20false%3B%20%20%2F%2F%20y%20%3D%200%3B%0A%0A%20%20%2F%2F%20Using%20bool%20in%20a%20numeric%20expression%3A%0A%20%20cout%20%3C%3C%202%20*%20(x%20%2B%20y)%20%3C%3C%20endl%3B%0A%20%20%0A%20%20return%200%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

Output

Leave a Reply

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