Return To CS 110 Home

Colour Theme   Font Size Options
 
   
   
   
Boolean Expressions

The bool Type and Boolean Expressions


Video Summary: https://youtu.be/Gx1L4Qy2DJs


Boolean Data Type — bool

In C++, data type bool is used to represent Boolean data. Each bool constant or variable contains one of two values:true or false.

true and false are two C++ constants.
true has the value 1.
false has the value 0.

If a testing expression is not of bool type,
it is coerced to bool type automaticaly when it is evaluated.
A nonzero value is coerced to true and a zero value is coerced to false.

Boolean Expressions

A Boolean expression can be a simple Boolean value, or it can be a more complex expression involving one or more relational and logical operators. Relational operators take two operands and test the relationship between them. The following table shows the relational operators and the corresponding C++ symbols.

Relational Operators
C++ Symbol Description
== equal to
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to

For example, The Boolean expression

number1 < number2

is evaluated to true if the value stored in number1 is less than the value stored in number2, and evaluated to false otherwise.

When a relational operator is applied between variables of type char, they are evaluated based on how they are encoded into the system, which is typically ASCII. ASCII encoding is alphabetical, with capital letters having lower values than lower case. For example,

character1 < character2

is evaluated to true if the value stored in character1 comes before the character stored in character2 on the ASCII chart

A simple Boolean expression is either a Boolean variable or an expression involving the relational operators that evaluates to either true or false. These simple Boolean expressions can be combined using logical operations defined on Boolean values. There are three Boolean operators: AND, OR and NOT. Here is a table showing how they are used in C++.
Logical Operators
C++ Symbol Description
&& The and Boolean operator:
  • And is a binary operator - placed between two boolean expressions.
  • If an operand is not already a boolean expression, it will be coerced to true or false.
  • If both operands are true, the result is true.
  • Otherwise, the result is false.
|| The or Boolean operator:
  • Or is a binary operator - placed between two boolean expressions.
  • If an operand is not already a boolean expression, it will be coerced to true or false.
  • If one or both operands are true, the result is true.
  • Otherwise, if both operants are false, the result is false.
! The not Boolean operator.
  • Not reverses the value of its operand
  • It is a unary operator placed before its operand.
  • If the operand is not already a boolean expresssion, it will be coerced to true or false.
  • If the operand is true, the result is false.
  • If the operand is false, the result is true.

Precedence of Operators

If relational operators and Boolean operators are combined in the same expression in C++, the Boolean operator NOT ! has the highest precedence, the relational operators have the next highest precedence, and the Boolean operators AND && and OR || have the lowest. Expressions in parentheses are always evaluated first.

The following table summarizes the precedence of all the C++ operators we have seen so far.

				Highest Precedence
					|
	(  )				|
					|
	++x  --x			|
					|
	!  Unary +   Unary -		|
					|
	*  /  %				|
					|
	+  -				|
					|
	<<   >>				|
					|
	<  <=  >   >=			|
					|
	==  !=				|
					|
	&&				|
					|
	||				|
					|
	=				|
					|
	x++  x--			|
					V
				Lowest Precedence

Operators in the same line in the table have the same precedence. If an expression contains several operators with the same precedence, most of the operators group from left to right. Some operators have different precedence based on where variables are in relation to them, for these an x represents the variable.

Short-circuit Evaluation

When a program evaluates control statements with multiple conditions, it reads from left to right. If the first condition is enough to determine the outcome, then the program will never check any of the following ones, which is called short-circuiting. For example:

(1 < 2 || 2 < 1)

Will return true, since 1 < 2 and || only requires one condition to be met, and never even check 2 < 1. An example with AND could be:

(2 < 1 && 1 < 2)

Again, since 2 < 1 is false and && needs all conditions to be met, it will never check 1 < 2.

It is important when writing control statements to make sure an important check is not skipped by short-circuiting, or else your program might crash later.