• Home
• /
• Blog
• /
• What is a Boolean Value? (Definition, Uses & Examples)

# What is a Boolean Value? (Definition, Uses & Examples)

October 17, 2020

In computer programs, there are three main types of data – text, numbers, and logical values. The text type of data includes the values such as name, address, etc., and the numbers hold the values such as age, marks obtained in a test, the salary of an employee, etc. The logical type of data includes values like true or false, yes or no.

The Boolean data type is used to store the logical values required in a computer program. Let’s understand what is a boolean value and what is a boolean variable with examples.

## What is a Data Type in Computer Science?

A data type, in programming, is a classification that specifies which type of value a variable has and what type of mathematical, relational, or logical operations can be applied to it without causing an error. A string, for example, is a data type that is used to classify text and an integer is a data type used to classify whole numbers.

The data type defines which operations can safely be performed to create, transform and use the variable in another computation. When a programming language requires a variable to only be used in ways that respect its data type, that language is said to be strongly typed.

This prevents errors, because while it is logical to ask the computer to multiply a float by an integer $\left(1.5 \times 5 \right)$, it is illogical to ask the computer to multiply a float by a string $\left(1.5 \times \text{Alice} \right)$. When a programming language allows a variable of one data type to be used as if it were a value of another data type, the language is said to be weakly typed.

## What is a Boolean Value?

In computer science, the Boolean (sometimes shortened to Bool) is a data type that has one of two possible values (usually denoted true and false) which is intended to represent the two truth values of logic and Boolean algebra.

It is named after George Boole, who first defined an algebraic system of logic in the mid-19th century. The Boolean data type is primarily associated with conditional statements, which allow different actions by changing control flow depending on whether a programmer-specified Boolean condition evaluates to true or false.

## History & Origins of the Boolean Data Type

Booleans are named after George Boole, who was a mathematician from the 19th century. He first developed Boolean algebra in 1854.

The Boolean data type was invented in the early 1800s. George Boole created a system of logic that could be used to describe the true values (i.e.: 1) and false values (i.e.: 0) in computers. This developed into what we now call Boolean algebra, which is used as a basis for most computer programming languages.

In 1854, George Boole wrote “An investigation into the Laws of Thought on Which are Founded the Mathematical Theories of Logic and Probabilities”, which laid the mathematical foundation for many computer languages.

Booleans were first used commercially in 1951 by a Bell Labs engineer as a part of AN/FSQ-7, the massive computer that was used by the United States Air Force.

## How are Boolean Values Used in Computer Programming?

A Boolean value is used to create conditions and control how a program behaves when certain things happen (e.g. if a condition is true, then do something). They can have only two possible values: either $0$ or $1$. You cannot add or subtract them. A Boolean variable is a special type of memory in a computer that can only store two values: true or false.

A boolean operator, or logical operator, consists of operators such as AND, OR, NOT, NOR, NAND, and XOR. These operators are used with conditional statements in programming, search engines, algorithms, and formulas.

## Difference Between Text, Numbers, and Booleans

These are the main differences between boolean, text, and numbers data types.

When using Boolean in programming, it’s important to understand the differences between a Boolean variable and other types of data. You need to know how they are stored, and what operations can be performed on them.

• Booleans: Booleans are usually stored using one byte of memory while text variables use more than one (e.g. two bytes for ASCII, four bytes for Unicode).
• Text: Some programming languages represent text with an array of characters (e.g. ASCII or Unicode). Text data types have a limited size (typically 256 characters) and do not perform operations on themselves.
• Numbers: Numbers can be negative, positive, or zero depending on their properties. They are stored as an array of bits which determines if they’re positive (i.e.: 00000000). The same applies to negative numbers (-1, -2, etc).

## Boolean Value Operators

When using Booleans in a program, it’s important to understand the Boolean operators. These are used in conditions and conditional statements which control how the program will behave. Operator examples include AND (&&), OR (||) and NOT (~).

The following are examples of the Boolean value operators in programming:

Boolean operators are used in making decisions in programs and indicate how the program should behave. For example, if $p$ is true AND $q$ is also true, then do something.

## Practice Problems

1. A boolean value can take either 1 or 2. (True/False)
2. A boolean value can take either True or False. (True/False)
3. A boolean value can take either 1 or 0. (True/False)
4. A boolean value can take either Yes or No. (True/False)
5. A boolean value is used with
• arithmetic operators
• logical operators
• conditional operators
• unary operators
6. The word boolean is named after __________.

## FAQs

### What are Boolean variables in programming?

The Boolean data type is used to store the values true and false. This data type may be used to store information that allows one of two states, on or off, to be stored.

### What is a nullable data type?

A nullable type is used to represent values that may be set to the null (no value) state.

### What is a null value?

null is the data type used to represent objects (e.g., variables, records, etc.) that do not currently exist, are unfinished/unused, or are being discarded so that resources will be freed. It could also be used when an expression evaluates to nothing and needs to be given a value. It returns false.

### What is a non-zero value?

A non-zero value is a value that is not zero. It returns true.

### Can a Boolean data type have the value of null?

No, it cannot, but you can use an undefined Boolean value to represent a null value. If checked against an operator, it returns false.

### What is a Boolean data type in a database?

Boolean data types can be used to store the values true and false in a database.
Booleans are most commonly used in databases to represent yes/no, on/off, or other related states. For example, if an account has been turned off then it’s status column may contain false. If it’s currently on the true would be stored in the account status Boolean. Another common use is for flags that allow you to store information about what happened (e.g.: ’email sent’, ‘file downloaded’).
Booleans may also be useful to show if a file exists, or if an action has been performed (e.g.: form submitted).

### What are some programming languages that support Boolean data types?

Booleans are available in most programming languages. If you’re using JavaScript, Java, PHP, Python, C, C++ or Swift then you should have access to the Boolean data type.
If null is allowed in your programming language, then both true and false would be allowed as well.

### What are some applications that manipulate Boolean data types?

Booleans can be used by application software to manage flags, yes/no options, on/off states, and other related information. They may also be useful in keeping track of resources or performing various tasks (e.g.: deleting files).

## Conclusion

In computer science, the Boolean (sometimes shortened to Bool) is a data type that has one of two possible values (usually denoted true and false) which is intended to represent the two truth values of logic and Boolean algebra. A Boolean value is used to create conditions and control how a program behaves when certain things happen.