Paul is a GIS professor at Vancouver Island U, has a PhD from U of British Columbia, and has taught stats and programming for 15 years.
Programming uses Booleans, which are used to represent values of true and false. Many operations use Boolean logic. Learn how Boolean operators and expressions are used.
Boolean Data Type
Programming uses a number of different data types. The data type of an object determines what type of values an object can have and what operations can be performed on the object. Commonly used data types include strings, numbers, lists and arrays. This lesson will look more closely at one data type that is widely used: Boolean data.
The Boolean data type can only represent two values: true or false. Typically, a 1 is used to represent true, and a 0 is used to represent false. Boolean data is widely used when working with conditions. If you want to ask a basic question and the answer can be only yes or no, you need a Boolean. The term 'Boolean' comes from a 19th century mathematician called George Boole who came up with the original idea of what we now call Boolean logic in his book The Laws of Thought.
Boolean data are used in Boolean expressions, which are expressions in a programming language that produce a Boolean value. An expression in programming is any combination of values, variables and operators that produce a new value. For example, 2 + 3 is an expression, and the result is the new value 5. When you use a Boolean expression, the only logical result can be true or false.
Consider the following example where a user inputs two values, and a computer program determines whether the first one is smaller than the second one or not.
x = 8 y = 7 x < y
In this example, the part 'x < y' is the Boolean expression. You are asking whether x is less than y, and the answer can only be a yes or a no - which means true or false in programming. In the example, the value of x is in fact not smaller than the value of y, and the program therefore results in a Boolean value of false. In programming language, we say that the expression is evaluated and returns a value of false.
The Boolean type is the primary result of conditional statements, which are used to control workflow in program. For example, if a particular condition is true, then do this; if the condition is false, then do something else.
In addition to Boolean data, there are Boolean operators, which are used to carry out Boolean algebra. There are three main Boolean operators: AND, OR and NOT. The first two are used to combine two expressions; the third is used as a negation operator. Let's look at each of these in more detail.
The simplest Boolean operator is the NOT operator. It simply turns true into false, and vice versa. Consider the following example.
x = 8 y = 7 NOT (x < y)
This returns a value of true. We know that x is greater than y, so the expression 'x < y' returns a value of false. The NOT operator turns this into a value of true.
Now, let's look at the AND and OR operators. The AND operator compares two expressions. It only returns a value of true if both expressions are true; otherwise, it returns a value of false. Consider the following example:
x = 8 y = 7 z = 6 (x < y) AND (z < y)
The first expression is false, and the second expression is true. The AND operator combines both expressions, and since one of them is false, the final result is false.
Over 79,000 lessons in all major subjects
Get access risk-free for 30 days,
just create an account.
Now, let's look at the OR operator. The OR operator also compares two expressions. It returns a value of true if one of the expressions is true or if both expressions are true. If both expressions are false, it returns a value of false.
x = 8 y = 7 z = 6 (x < y) OR (z < y)
The first expression is false, and the second expression is true. The OR operator combines both expressions, and since one of them is true, the final result is true.
These Boolean operators illustrate the use of Boolean logic. Boolean logic is widely used when writing programs. It is also widely used as part of search algorithms and database queries.
For example, when you use a search engine, you can go into advanced settings to give you more control. One option is to search for all these words; you want to find pages where all these words occur together. This is similar to using a Boolean AND operator, since you want the results for finding those words on the same page to be true. This is typically the default for any search engine.
Another option is to search for any of these words; you want to find the page where one or more of these words occur, but they don't all have to occur together. This is similar to using a Boolean OR operator, since you don't need the results to contain all of these words together; it just needs to be true for one of them. So, you are using Boolean operators every time you do an online search using two or more words. So, now you know a bit more about how those search engines actually work.
The Boolean data type can only represent two values: true or false. Boolean expressions are expressions in a programming language that produce a Boolean value. This is like asking a question where the logical answers can only be true or false.
Boolean operators are used to carry out Boolean algebra. The three main Boolean operators are AND, OR and NOT. The first two are used to combine two expressions, and the third is used to return the opposite value. Boolean logic is used in programming, search engines and database queries.
After you've reviewed this video lesson, you should be able to:
Define Boolean data, Boolean expressions and Boolean operators
Explain the uses of the three main Boolean operators
Did you know… We have over 200 college
courses that prepare you to earn
credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the
first two years of college and save thousands off your degree. Anyone can earn
credit-by-exam regardless of age or education level.