Boolean Logic

The bool type introduced in the previous tutorials can hold one of only two values: true or false . This type is often used to record the result of some operation, so that you can act on this result. In particular, bool types are used to store the result of a comparison .
For instance, consider the situation in which you want to execute code based on whether a variable, myVal , is less than 10. To do this, you need some indication of whether the statement “ myVal is less than 10 ” is true or false — that is, you need to know the Boolean result of a comparison.
Boolean comparisons require the use of Boolean comparison operators (also known as relational operators ), which are shown in the following table. In all cases here, var1 is a bool type variable, whereas the types of var2 and var3 may vary.

Operator Category Example Expression Result
== Binary var1=var2==var3; var1 is assigned the value true if var2 is equal to var3 , or false otherwise.
!= Binary var1 = var2 != var3; var1 is assigned the value true if var2 is not equal to var3 , or false otherwise.
< Binary var1 = var2 < var3; var1 is assigned the value true if var2 is less than var3 , or false otherwise.
> Binary var1 = var2 > var3; var1 is assigned the value true if var2 is greater than var3 , or false otherwise.
<= Binary var1 = var2 < = var3; var1 is assigned the value true if var2 is less than or equal to var3 , or false otherwise.
>= Binary var1 = var2 > = var3; var1 is assigned the value true if var2 is greater than or equal to var3 , or false otherwise.

You might use operators such as these on numeric values in code:

bool isLessThan10;
isLessThan10 = myVal < 10;

This code results in isLessThan10 being assigned the value true if myVal stores a value less than 10, or false otherwise.
You can also use these comparison operators on other types, such as strings:

bool isKarli;
isKarli = myString == “Karli”;

Here, isKarli is true only if myString stores the string “ Karli ” .
You can also focus on Boolean values:

bool isTrue;
isTrue = myBool == true;

Here, however, you are limited to the use of == and != operators. A common code error occurs if you unintentionally assume that because val1 < val2 is false, val1 > val2 is true. If val1 == val2 , then both these statements are false. Some other Boolean operators are intended specifically for working with Boolean values, as shown in the following table:

Operator Category Example Expression Result
! Unary var1 = ! var2; var1 is assigned the value true if var2 is false , or false if var2 is true . (Logical NOT )
& Binary var1 = var2 & var3; var1 is assigned the value true if var2 and var3 are both true , or false otherwise. (Logical AND )
| Binary var1 = var2 | var3; var1 is assigned the value true if either var2 or var3 (or both) is true , or false otherwise. (Logical OR )
^ Binary var1 = var2 ^ var3; var1 is assigned the value true if either var2 or var3 , but not both, is true , or false otherwise. (Logical XOR or exclusive OR )

Therefore, the previous code snippet could also be expressed as follows:

bool isTrue;
isTrue = myBool & true;

The & and | operators also have two similar operators, known as conditional Boolean operators, shown in the following table:

Operator Category Example Expression Result
& & Binary var1 = var2 & &
var3;
var1 is assigned the value true if var2 and var3 are both true , or false otherwise. (Logical AND )
|| Binary var1 = var2 ||
var3;
var1 is assigned the value true if either var2 or var3 (or both) is true , or false otherwise. (Logical OR )

The result of these operators is exactly the same as & and | , but note an important difference in the way this result is obtained, which can result in better performance. Both of these look at the value of their first operands ( var2 in the preceding table) and, based on the value of this operand, may not need to process the second operators ( var3 in the preceding table) at all.
 
If the value of the first operand of the & & operator is false , then there is no need to consider the value of the second operand, because the result will be false regardless. Similarly, the || operator returns true if its first operand is true , regardless of the value of the second operand. This isn ’ t the case for the & and | operators shown earlier. With these, both operands are always evaluated.
 
Because of this conditional evaluation of operands, you get a small performance increase if you use & & and || instead of & and | . This is particularly apparent in applications that use these operators a lot. As a rule of thumb, always use & & and || where possible. These operators really come into their own in more complicated situations, where computation of the second operand is possible only with certain values of the first operand, as shown in this example:

var1 = (var2 != 0) & & (var3 / var2 > 2);

Here, if var2 is zero, then dividing var3 by var2 results in either a “ division by zero ” error or var1 being defined as infinite (the latter is possible, and detectable, with some types, such as float ).