8F. Booleans Are Actually Integers

C++ is different from Java in that C++ type bool is actually equivalent to type int. Constant true is 1 and constant false is 0.

It is considered good practice, though, to write true and false in your program for boolean values rather than 1 and 0. The coding standards for this course require you to do that.


Testing integer values

Whenever an integer value is tested to see whether it is true of false, 0 is considered to be false and all other integers are considered be true. For example, in

  if(4)
  {
    
  }
4 is treated like true. But don't do that in your program. The standards for this course require you to test values that are true or false, with rare exceptions.


Boolean operators

Operators !, && and || always yield either true or false. For example, expression 3 && -2 yields true.

That is what C++ does. The coding standards for this course require that boolean operators only be used when their operands are either true or false. For example, expression 1 || 2 violates that standard.