Boolean Operators

FIELD OF STUDY

Software Development; Coding Techniques; Computer Science

ABSTRACT

Boolean, or logical, operators are used to create Boolean expressions, which test whether a condition is true or false. Statements such as the if-statement use Boolean expressions to control the order in which a computer program processes commands and other statements.

PRINCIPAL TERMS

In computer programming, an expression describes some value (much as a math equation does), often using integers, strings, constants, or variables. Boolean expressions are expressions that, when evaluated, return one of two values: “true/false,” “yes/ no,” or “on/off.” For example, the Boolean expression x > 5 would return a value of “false” if x = 4 and a value of “true” if x = 6. Boolean expressions can be used with different data types, including integers, strings, and variables. Complex Boolean expressions can be created using Boolean operators. Boolean operators combine two or more Boolean expressions into a single expression and can also reverse the value of a Boolean expression.

There are three main Boolean operators: AND, OR, and NOT. Different programming languages use different symbols to represent them, such as && for AND or || for OR in Java, but the uses remain the same. The AND operator combines two Boolean expressions into a single expression that evaluates to true if, and only if, both expressions are true. If either expression evaluates to false, then the combined expression is false. When two expressions are joined by the OR operator, the combined expression evaluates to true if either expression is true and only evaluates to false if both are false.

The NOT operator reverses the value of a Boolean expression. For example, the expression x > 5 evaluates to true if the value of x is greater than 5. However, the NOT operator can be used to reverse this result. The expression NOT x > 5 evaluates to false if the value of x is greater than 5.

The order in which operations are performed is called “precedence.” Arithmetic is done before Boolean operations. Boolean operators with higher precedence are evaluated before those with lower precedence: NOT is evaluated before AND, which is evaluated before OR. The default precedence can be overridden using nested parentheses. Expressions within the innermost parentheses are evaluated first, followed by expressions contained within the parentheses that surround them, and so on. However, when using the AND operator, if any component expression evaluates to false, then any component expressions that follow the false expression are not evaluated. Likewise, when using the OR operator, if a component expression evaluates to true, then any component expressions that follow the true expression are not evaluated. This is called “short-circuiting.”

The order in which statements are executed is called “control flow.” Statements such as the if-statement and if-else statement alter a program's control flow. The if-statement tests for a condition, and if that condition is true, control flow passes to a designated block of code. If the condition is false, no action is performed. In an if-else statement, if the condition is false, control flow passes to a different block of code. Boolean expressions are used to create the conditions used by if-statements and if-else statements.




Boolean Operators

However, as Boolean expressions become more complicated, they become increasing difficult to understand correctly. For example, the expression NOT x < 5 AND y > 1 OR country = “UK” can only be executed correctly if the order of precedence for the operators is known. Programmers use parentheses to define the order precedence they desire to make such expressions easier to understand. For example, the above expression could be rewritten as follows:




Boolean Operators

CONDITIONAL STATEMENTS IN PRACTICE

If statements and if-else statements could be used to assign a letter grade based on an exam score. First, a Boolean expression is used to check that the exam has been completed and scored. The AND operator is used because both conditions must be true for a letter grade to be assigned.




Boolean Operators

Next, the code uses a chain of if statements to assign a letter grade to the variable Grade. Note how the first statement does not use a Boolean operator because it does not need to evaluate more than one expression. The next three statements use the Boolean operator AND because they require both expressions to be true for the code to be executed. The final if-statement does not contain a Boolean expression because it executes only if all other if-statements fail to execute.




Boolean Operators

Boolean operators are commonly used when searching for information in a database. For instance, if a student needs to write a research paper on climate change, they might type in “global warming OR climate change.” This would tell the library's database to return results that include either phrase and thus increase the overall number of results returned.

Boolean operators are also used to program software in which a sequential flow of control is not desired. The conditions that they enable allow programs not only to branch through selection, but also to loop (or repeat) sections of code. Thus, Boolean operators allow more complex software to be developed.

—Maura Valentino, MSLIS

Friedman, Daniel P., and Mitchell Wand. Essentials of Programming Languages. 3rd ed., MIT P, 2008.

Haverbeke, Marijn. Eloquent JavaScript: A Modern Introduction to Programming. 2nd ed., No Starch Press, 2014.

MacLennan, Bruce J. Principles of Programming Languages: Design, Evaluation, and Implementation. 3rd ed., Oxford UP, 1999.

Schneider, David I. An Introduction to Programming using Visual Basic. 10th ed., Pearson, 2016.

Scott, Michael L. Programming Language Pragmatics. 4th ed., Morgan Kaufmann Publishers, 2016.

Van Roy, Peter, and Seif Haridi. Concepts, Techniques, and Models of Computer Programming. MIT P, 2004.