In
computer scienceComputer science or computing science is the study of the theoretical foundations of information and computation and of practical techniques for their implementation and application in computer systems...
, the
Boolean or
logical data type is a
data typeIn computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...
, having two values (usually denoted
true and
false), intended to represent the truth values of
logicIn philosophy, Logic is the formal systematic study of the principles of valid inference and correct reasoning. Logic is used in most intellectual activities, but is studied primarily in the disciplines of philosophy, mathematics, semantics, and computer science...
and Boolean algebra. It is named after
George BooleGeorge Boole was an English mathematician and philosopher.As the inventor of Boolean logic—the basis of modern digital computer logic—Boole is regarded in hindsight as a founder of the field of computer science. Boole said,...
, who first defined an algebraic system of logic in the mid 19th century.
Implementations
In
programming languageA programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
s that have a built-in Boolean data type, such as Pascal and Java, the comparison operators such as '>' and '≠' are usually defined to return a Boolean value. Also, conditional and
iterative commandIn most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement....
s may be defined to test Boolean-valued expressions.
Languages without an explicit Boolean data type, like C90 and Lisp, may still represent truth values by some other data type. Lisp uses an empty list for false, and any other value for true. C uses an
integerIn computer science, an integer is a datum of integral data type, a data type which represents some finite subset of the mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values....
type, where relational expressions like
i > j and logical expressions connected by
&& and
|| are defined to have value 1 if true and 0 if false, whereas the test parts of
if,
while,
for, etc., treat any non-zero value as true. Indeed, a Boolean variable may be regarded (and be implemented) as a numerical variable with a single
binary digit (bit)A bit is the basic unit of information in computing and telecommunications; it is the amount of information stored by a digital device or other physical system that exists in one of two possible distinct states...
, which can store only two values.
Most programming languages, even those that do not have an explicit Boolean type, have support for Boolean algebraic operations such as
conjunctionIn logic and mathematics, a two-place logical operator and, also known as logical conjunction, results in true if both of its operands are true, otherwise the value of false....
(
AND,
&,
*),
disjunctionIn logic and mathematics, a two-place logical connective or, is a logical disjunction, also known as inclusive disjunction or alternation, that results in true whenever one or more of its operands are true. E.g. in this context, "A or B" is true if A is true, or if B is true, or if both A and B are...
(
OR,
|,
+),
equivalenceIn logic, statements p and q are logically equivalent if they have the same logical content.Syntactically, p and q are equivalent if each can be proved from the other...
(
EQV,
=,
),
exclusive or/non-equivalenceThe logical operation exclusive disjunction, also called exclusive or , is a type of logical disjunction on two operands that results in a value of true if exactly one of the operands has a value of true...
(
XOR,
NEQV,
^,
!=), and
notIn logic and mathematics, negation, also called logical complement, is an operation on propositions, truth values, or semantic values more generally. Intuitively, the negation of a proposition is true when that proposition is false, and vice versa. In classical logic negation is normally identified...
(
NOT,
~,
!).
In some languages, like
RubyRuby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...
and
SmalltalkSmalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist...
, the "true" and "false" values belong to separate classes -- e.g. "True" and "False", respective -- so there is no single Boolean "type".
In some languages, the Boolean data type is defined to include more than two truth values. For instance the
ISO SQL 1999 standardSQL:1999 was the fourth revision of the SQL database query language. The latest revision of the standard is SQL:2008.-Summary:The SQL:1999 standard, also known as SQL3, was published in 1999. Unlike previous editions, the standard's name used a colon instead of a hyphen for consistency with the...
defined a Boolean value as being either true, false, or unknown (
SQL nullNull is a special marker used in Structured Query Language to indicate that a data value does not exist in the database. Introduced by the creator of the relational database model, E. F. Codd, SQL Null serves to fulfill the requirement that all true relational database management systems support...
). Although this convention violates the
law of excluded middleIn logic, the law of excluded middle is the third of the so-called three classic laws of thought. It states that for any proposition, either that proposition is true, or its negation is....
, it is often useful in programming.
In the
lambda calculusIn mathematical logic and computer science, lambda calculus, also written as λ-calculus, is a formal system for function definition, function application and recursion. The portion of lambda calculus relevant to computation is now called the untyped lambda calculus...
model of computing, Boolean values can be represented as Church booleans.
History
One of the earliest languages to provide an explicit
Boolean data type was
ALGOL 60ALGOL 60 is a member of the ALGOL family of computer programming languages. It gave rise to many other programming languages, including BCPL, B, Pascal, Simula, C, and many others. ALGOL 58 introduced code blocks and the begin and end pairs for delimiting them...
(1960) with values
true and
false and logical operators denoted by symbols '

' (and), '

' (or), '

' (implies), '

' (equivalence), and '

' (not). Due to input device limitations of the time, however, most compilers used alternative representations for the latter, such as
AND or
'AND'. This approach ("Boolean is a separate built-in primitive data type") was adopted by many later languages, such as
ALGOL 68ALGOL 68 isan imperative computerprogramming language that was conceived as a successor to theALGOL 60 programming language, designed with the goal of a...
(1970),
JavaJava is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
, and C#.
The first version of
FORTRANFortran is a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing...
(1957) and its successor FORTRAN II (1958) did not have logical values or operations; even the conditional
IF statement took an arithmetic expression and branched to one of three locations according to its sign. FORTRAN IV (1962), however, followed the ALGOL 60 example by providing a Boolean data type (
LOGICAL), truth literals (
.TRUE. and
.FALSE.), Boolean-valued numeric comparison operators (
.EQ.,
.GT., etc.), and logical operators (
.NOT.,
.AND.,
.OR.). In
FORMAT statements, a specific control character ('
L') was provided for the parsing or formatting of logical values.
The Lisp language (1958) never had a built-in Boolean data type. Instead, conditional constructs like
cond assume that the logical value "false" is represented by the empty list
, which is defined to be the same as the special atom
nil or
NIL; whereas any other
s-expressionS-expressions or sexps are list-based data structures that represent semi-structured data. An S-expression may be a nested list of smaller S-expressions. S-expressions are probably best known for their use in the Lisp family of programming languages...
is interpreted as "true". For convenience, most modern dialects of Lisp predefine the atom
t to have value
t, so that one can use
t as a mnemonic notation for "true". This approach ("any value can be used as a Boolean value") was retained in most Lisp dialects (
Common LispCommon Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...
, Scheme,
Emacs LispEmacs Lisp is a dialect of the Lisp programming language used by the GNU Emacs and XEmacs text editors . It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C...
), and similar models were adopted by many
scripting languageA scripting language, script language, or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the...
s, even ones that do have a distinct Boolean type or Boolean values; although which values are interpreted as "false" and which are "true" vary from language to language. In Scheme, for example, the "false" value is an atom distinct from the empty list, so the latter is interpreted as "true". In
PythonPython is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
, a numeric value of zero (integer or fractional), the null value (
None), the empty
stringIn formal languages, which are used in mathematical logic and theoretical computer science, a string is a finite sequence of symbols that are chosen from a set or alphabet....
, and empty containers (i.e.
lists, sets, etc.) are considered Boolean false; all other values are considered Boolean true by default. In
RubyRuby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...
, on the other hand, only the null object and a special
false object are "false", everything else (including the integer 0 and empty arrays) is "true". In
JavaScriptJavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....
, the empty string (
""),
null,
undefined,
NaNIn computing, NaN is a value of the numeric data type representing an undefined or unrepresentable value, especially in floating-point calculations...
, +0, −0 and
false
are sometimes called "falsy", and their
complementIn set theory, a complement of a set A refers to things not in , A. The relative complement of A with respect to a set B, is the set of elements in B but not in A...
, "truthy", to distinguish between strictly type-checked and coerced Booleans. Languages such as
PHPPHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...
also use this approach.
The initial standards for the
CC is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
language (1972) provided no Boolean type; and, to this day, Boolean values are commonly represented by integers (
ints) in C programs. The comparison operators ('
>', '
', etc.) are defined to return a signed integer (
int) result, either zero (for false) or 1 (for true). The same convention is assumed by the logical operators ('
&&', '
||', '
!', etc.) and condition-testing statements ('
if', '
while'). Thus logical values can be stored in integer variables, and used anywhere integers would be valid, including in indexing, arithmetic, parsing, and formatting. This approach ("Boolean values are just integers") was retained in all later versions of C. Some of its dialects, like
C99C99 is a modern dialect of the C programming language. It extends the previous version with new linguistic and library features, and helps implementations make better use of available computer hardware and compiler technology.-History:...
and
Objective-CObjective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...
, provide standard definitions of a Boolean type as an integer type and macros for "false" and "true" as 0 and 1, respectively.
Visual BasicVisual Basic is the third-generation event-driven programming language and integrated development environment from Microsoft for its COM programming model...
uses a similar approach.
C++C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
has a separate Boolean data type (
'bool'), but with automatic conversions from scalar and pointer values that are very similar to those of C. This approach was adopted also by many later languages, especially by some
scriptingA scripting language, script language, or extension language is a programming language that allows control of one or more applications. "Scripts" are distinct from the core code of the application, as they are usually written in a different language and are often created or at least modified by the...
ones such as AWK and
PerlPerl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
. One problem with this approach is that the tests
if(tTRUE){...} and
if(t) are not equivalent. Python has a related situation, where the Boolean type,
bool is a subtype of the integer type,
int, and Booleans False and True act as 0 and 1, respectively, in arithmetic contexts.
The
PascalPascal is an influential imperative and procedural programming language, designed in 1968/9 and published in 1970 by Niklaus Wirth as a small and efficient language intended to encourage good programming practices using structured programming and data structuring.A derivative known as Object Pascal...
language (1978) introduced the concept of programmer-defined
enumerated typeIn computer programming, an enumerated type is a data type consisting of a set of named values called elements, members or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language...
s. A built-in
Boolean data type was then provided as a predefined enumerated type with values
FALSE and
TRUE. By definition, all comparisons, logical operations, and conditional statements applied to and/or yielded
Boolean values. Otherwise, the
Boolean type had all the facilities which were available for enumerated types in general — such as ordering and use as indices. On the other hand, the conversion between
Booleans and integers (or any other types) still required explicit tests or function calls, as in ALGOL 60. This approach ("Boolean is an enumerated type") was adopted by most later languages which had enumerated types, such as
ModulaThe Modula programming language is a descendent of the Pascal programming language. It was developed in Switzerland in the late 1970s by Niklaus Wirth, the same person who designed Pascal...
,
AdaAda is a structured, statically typed, imperative, wide-spectrum, and object-oriented high-level computer programming language, extended from Pascal and other languages...
and
HaskellHaskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...
.
After enumerated types (
enums) were added to the
ANSI version of CANSI C refers to the family of successive standards published by the American National Standards Institute for the C programming language. Software developers writing in C are encouraged to conform to the standards, as doing so aids portability between compilers.-History and outlook:The first...
(1989), many C programmers got used to defining their own Boolean types as such, for readability reasons. However, enumerated types are equivalent to integers according to the language standards; so the effective identity between Booleans and integers is still valid for C programs.
Special approaches
In recent versions of Python, user-defined objects may specify their truth value by providing a
__bool__ method.
As of the 1999 standard,
SQLSQL is a programming language designed for managing data in relational database management systems ....
specified a Boolean data type with four possible values: true, false, unknown or null. However, vendors could choose to equate the last two values. Because of this inconsistency most SQL implementations (with the notable exception of
PostgresqlPostgreSQL, often simply Postgres, is an object-relational database management system available for many platforms including Linux, FreeBSD, Solaris, MS Windows and Mac OS X. It is released under the PostgreSQL License, which is an MIT-style license, and is thus free and open source software...
) use other data types (like
bitA bit is the basic unit of information in computing and telecommunications; it is the amount of information stored by a digital device or other physical system that exists in one of two possible distinct states...
,
byteThe byte is a unit of digital information in computing and telecommunications that most commonly consists of eight bits. Historically, a byte was the number of bits used to encode a single character of text in a computer and for this reason it is the basic addressable element in many computer...
, and
characterIn computer and machine-based telecommunications terminology, a character is a unit of information that roughly corresponds to a grapheme, grapheme-like unit, or symbol, such as in an alphabet or syllabary in the written form of a natural language....
) to simulate Boolean values.
See also
- true and false
In Unix-like operating systems, true and false are commands whose only function is to always return the value 0 or 1.-Usage:The exit status of a command is often used to indicate success or failure of the command. The true and false commands represent the logical value of command success, because...
commands for shell scripting
- Shannon's expansion
- Stdbool.h — C99 definitions for boolean