Skip to main content

Section 2.3 Boolean Data

Boolean data types are named after George Boole who was an English mathematician, so the word “Boolean” should be capitalized. However, the Boolean data type, in C++ uses the keyword bool which is not capitalized. The possible state values for a C++ Boolean are lower case true and false. Be sure to note the difference in capitalization from Python. In Python, these same truth values are capitalized, while in C++, they are lower case.
C++ uses the standard Boolean operators, but they are represented differently than in Python: “and” is given by && , “or” is given by || , and “not” is given by !. Note that the internally stored values representing true and false are actually 1 and 0 respectively. Hence, we see this in output as well.
Boolean data objects are also used as results for comparison operators such as equality (==) and greater than (>). In addition, relational operators and logical operators can be combined together to form complex logical questions. Table 2.3.1 shows the relational and logical operators with examples shown in the session that follows.
Table 2.3.1.
Operation Name Operator Explanation
less than < Less than operator
greater than > Greater than operator
less than or equal <= Less than or equal to operator
greater than or equal >= Greater than or equal to operator
equal == Equality operator
not equal != Not equal operator
logical and && Both operands true for result to be true
logical or || One or the other operand is true for the result to be true
logical not ! Negates the truth value, false becomes true, true becomes false
When a C++ variable is declared, space in memory is set aside to hold this type of value. A C++ variable can optionally be initialized in the declaration by using a combination of a declaration and an assignment statement.
Consider the following session:
The declaration int theSum = 4; creates a variable called theSum and initializes it to hold the data value of 4git. As in Python, the right-hand side of each assignment statement is evaluated and the resulting data value is “assigned” to the variable named on the left-hand side. Here the type of the variable is integer. Because Python is dynamically typed, if the type of the data changes in the program, so does the type of the variable. However, in C++, the data type cannot change. This is a characteristic of C++’s static typing. A variable can hold ever only one type of data. Pitfall: C++ will often simply try to do the assignment you requested without complaining. Note what happened in the code above in the final output.

Reading Questions Reading Question

1.

Why did theBool output a value of 1 after being set to 4?
  • Setting theBool to anything other than true or false is ignored.
  • No. Try changing the code and setting theBool to 0.
  • Setting theBool to anything > 0 will be true and false otherwise.
  • No. Try changing the code and setting theBool to -4.
  • false == 0 and true = !false i.e. anything that is not zero and can be converted to a Boolean is not false, thus it must be true.
  • Correct!
  • None of the above.
  • Try again. One of the above is correct. You might try changing the code and rerunning.
You have attempted 1 of 7 activities on this page.