In computer science, the Boolean or logical data type is a data type, having two values,
intended to represent the 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 the primary result of conditional statements, which allow different
actions and change control flow depending on whether a programmer-specified boolean
condition evaluates to true or false.
Generalities In programming languages 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. Conditional and iterative commands 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 integer 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 as a numerical variable
with a single binary digit, which can store only two values. It is worth noting that the
implementation of booleans in computers are most likely represented as a full word, rather
than a bit; this is usually due to the ways computers transfer blocks of information.
Most programming languages, even those that do not have an explicit Boolean type, have
support for Boolean algebraic operations such as conjunction, disjunction, equivalence,
exclusive or/non-equivalence, and negation. In some languages, like Ruby, Smalltalk, and
Alice the "true" and "false" values belong to separate classes—e.g. True and False,
resp.—so there is no single Boolean "type." In SQL, which uses a three-valued logic for
explicit comparisons because of its special treatment of Nulls, the Boolean data type
is also defined to include more than two truth values, so that SQL "Booleans" can store all
logical values resulting from the evaluation of predicates in SQL. A column of Boolean
type can also be restricted to just TRUE and FALSE though.
In the lambda calculus model of computing, Boolean values can be represented as Church
booleans. ALGOL, Java, and C#
One of the earliest programming languages to provide an explicit Boolean data type was
ALGOL 60 with values true and false and logical operators denoted by symbols '', '', '', '', and
''. Due to input device limitations of the time, however, most compilers used alternative
representations for the latter, such as AND or 'AND'.
This approach was adopted by many later programming languages, such as ALGOL 68, Java, and C#.
Fortran The first version of FORTRAN and its successor
FORTRAN II 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; see arithmetic IF. FORTRAN IV, however, followed the ALGOL 60 example by providing
a Boolean data type, truth literals, Boolean-valued numeric comparison operators, and logical
operators. In FORMAT statements, a specific control character was provided for the parsing
or formatting of logical values. Lisp and Scheme
The Lisp language 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-expression
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 was retained in most Lisp dialects, and similar models were adopted by many scripting
languages, 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". Pascal, Ada, and Haskell
The Pascal language introduced the concept of programmer-defined enumerated types. 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 still required
explicit tests or function calls, as in ALGOL 60. This approach was adopted by most later
languages which had enumerated types, such as Modula, Ada and Haskell.
C, C++, Objective-C, AWK, Perl The initial implementations of the C language
provided no Boolean type, and to this day Boolean values are commonly represented by
integers in C programs. The comparison operators are defined to return a signed integer result,
either 0 or 1. Logical operators and condition-testing statements assume that zero is false and all
other values are true. One problem with this approach is that the tests if(t==TRUE){...}
and if(t) are not equivalent. After enumerated types were added to the ANSI
version of C, 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. Standard C and several dialects of C such
as Objective-C provide definitions of a Boolean type as an integer type and macros for "false"
and "true" as 0 and 1, respectively. 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 has been retained in all later versions of C.
C++ has a separate Boolean data type, 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 scripting ones such as AWK.
Objective-C also has a separate Boolean data type, with possible values being YES or NO,
equivalents of true and false respectively. Perl has many values of false: the number
zero, the strings "0" and "", the empty list "()", and the special value undef. Everything
else evaluates to true. Python, Ruby, and JavaScript
In Python from version 2.3 forward, there is a bool type which is a subclass of int,
the standard integer type. It has two possible values: True and False, which are "special
versions" of 1 and 0 respectively and behave as such in arithmetic contexts. In addition,
a numeric value of zero, the null value, the empty string, and empty containers are considered
Boolean false; all other values are considered Boolean true by default. Classes can define
how their instances are treated in a Boolean context through the special method __nonzero__
or __bool__. For containers, __len__ is used if the explicit Boolean conversion method
is not defined. In Ruby, on the other hand, only nil and a
special false object are "false", everything else is "true".
In JavaScript, the empty string, null, undefined, NaN, +0, −0 and false are sometimes called
"falsy", and their complement, "truthy", to distinguish between strictly type-checked
and coerced Booleans. Languages such as PHP also use this approach.
SQL The SQL:1999 standard introduced a BOOLEAN
data type as an optional feature. When restricted by a NOT NULL constraint, a SQL BOOLEAN behaves
like Booleans in other languages. In SQL however, the BOOLEAN type is nullable by default like
all other SQL data types, meaning it can have the special null value as well. Although the
SQL standard defines three literals for the BOOLEAN type—TRUE, FALSE and UNKNOWN—,
it also says that the NULL BOOLEAN and UNKNOWN "may be used interchangeably to mean exactly
the same thing". This has caused some controversy because the identification subjects UNKNOWN
to the equality comparison rules for NULL. More precisely UNKNOWN = UNKNOWN is not TRUE
but UNKNOWN/NULL. As of 2012 few major SQL systems implement the T031 feature. PostgreSQL
is a notable exception, although it does not implement the UNKNOWN literal; NULL can be
used instead. In other SQL implementations various ad hoc solutions are used, like bit,
byte, and character to simulate Boolean values. See also
true and false commands for shell scripting Shannon's expansion
stdbool.h — C99 definitions for boolean References