Boolean data type
≠are usually defined to return a Boolean value. Conditional and iterative commands may be defined to test Boolean-valued expressions.
i > jand logical expressions connected by
||are defined to have value 1 if true and 0 if false, whereas the test parts of
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), 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.
+), equivalence (
==), exclusive or/non-equivalence (
!=), and negation (
False, resp.—so there is no single Boolean "type."
ALGOL and the built in boolean type
IFstatement took an arithmetic expression and branched to one of three locations according to its sign; see arithmetic IF. FORTRAN IV (1962), however, followed the ALGOL 60 example by providing a Boolean data type (
LOGICAL), truth literals (
.FALSE.), Boolean-valued numeric comparison operators (
.GT., etc.), and logical operators (
FORMATstatements, a specific control character ('
L') was provided for the parsing or formatting of logical values.
Lisp and Scheme
condassume that the logical value "false" is represented by the empty list
(), which is defined to be the same as the special atom
NIL; whereas any other s-expression is interpreted as "true". For convenience, most modern dialects of Lisp predefine the atom
tto have value
t, so that one can use
tas a mnemonic notation for "true".
Pascal, Ada, and Haskell
Booleandata type was then provided as a predefined enumerated type with values
TRUE. By definition, all comparisons, logical operations, and conditional statements applied to and/or yielded
Booleanvalues. Otherwise, the
Booleantype 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 Modula, Ada and Haskell.
C, C++, Objective-C, AWK
ints) in C programs. The comparison operators (
==, etc.) are defined to return a signed integer (
int) result, either 0 (for false) or 1 (for true). Logical operators (
!, etc.) and condition-testing statements (
while) assume that zero is false and all other values are true.
enums) were added to the ANSI version of C (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.
_Bool. By including the header
stdbool.hone can use the more intuitive name
booland the constants
false. The language guarantees that any two true values will compare equal (which was impossible to achieve before the introduction of the type). Boolean values still behave as integers, 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") has been retained in all later versions of C.
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 scripting ones such as AWK.
BOOL, with possible values being
NO, equivalents of true and false respectively. In addition, in Objective-C compilers that support C99, the C's
_Booltype can be used since Objective-C is a superset of C.
whilestatement, argument of
||, etc.). The number
0, the strings
"", the empty list
(), and the special value
undefevaluate to false. Everything else evaluates to true.
booltype which is a subclass of
int, the standard integer type. It has two possible values:
False, which are "special versions" of 1 and 0 respectively and behave as such in arithmetic contexts. In addition, a numeric value of zero (integer or fractional), the null value (
None), the emptystring, and empty containers (i.e. lists, sets, etc.) 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__(Python 2) or
__bool__(Python 3). For containers,
__len__(the special method for determining the length of containers) is used if the explicit Boolean conversion method is not defined.
nil(Ruby's null value) and a special
falseobject are "false", everything else (including the integer 0 and empty arrays) is "true".
NaN, +0, −0 and
false are sometimes called "falsy", and their complement, "truthy", to distinguish between strictlytype-checked and coerced Booleans. Languages such as PHP also use this approach.
NOT NULLconstraint, 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.