Boolean data type


In computer science, the Boolean is a data type that has one of two possible values 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. It is a special case of a more general logical data type—logic does not always need to be Boolean .

Generalities

In programming languages with a built-in Boolean data type, such as Pascal, C, Python or 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 with no explicit Boolean data type, like C90 and Lisp, may still represent truth values by some other data type. Common Lisp uses an empty list for false, and any other value for true. The C programming language 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 one binary digit, or as a bit string of length one, which can store only two values. 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 with no 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, respectively, so there is no one 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 be restricted to just TRUE and FALSE though.

Language-specific implementations

ALGOL and the built-in BOOLEAN type

One of the earliest programming languages to provide an explicit BOOLEAN data type is ALGOL 60 with values true and false and logical operators denoted by symbols , , , , and ''. Due to input device and character set limits on many computers of the time, however, most compilers used alternative representations for many of the operators, such as AND or 'AND'.
This approach with BOOLEAN as a built-in data type was adopted by many later programming languages, such as Simula 67, ALGOL 68, Pascal, Ada, Java, and C#, among others.

C, C++, D, Objective-C, AWK

Initial implementations of the language C 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.
After enumerated types were added to the American National Standards Institute version of C, ANSI 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 provides a Boolean type, called _Bool. Since C23, the Boolean is now a core data type called bool, with values true and false. The language guarantees that any two true values will compare equal. 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 has been retained in all later versions of C. Note, that this does not mean that any integer value can be stored in a Boolean variable.
C++ has had the Boolean data type bool since C++98, 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 languages such as AWK.
The D programming language has a proper Boolean data type bool. The bool type is a byte-sized type that can only hold the value true or false.
The only operators that can accept operands of type bool are: and.
A bool value can be implicitly converted to any integral type, with false becoming 0 and true becoming 1.
The numeric literals 0 and 1 can be implicitly converted to the bool values false and true, respectively. Casting an expression to bool means testing for 0 or for arithmetic types, and or for pointers or references.
Objective-C also has a separate Boolean data type BOOL, with possible values being YES or NO, equivalents of true and false respectively. Also, in Objective-C compilers that support C99, C's _Bool type can be used, since Objective-C is a superset of C.

Forth

has no Boolean type, it uses regular integers: value 0 represents false, and -1 represents true. This allows the language to define only one set of logical operators, instead of one for mathematical calculations and one for conditions.

Fortran

The first version of FORTRAN and its successor FORTRAN II have no logical values or operations; even the conditional IF statement takes an arithmetic expression and branches to one of three locations according to its sign; see arithmetic IF. FORTRAN IV, however, follows the ALGOL 60 example by providing a Boolean data type, truth literals, logical IF statement, Boolean-valued numeric comparison operators, and logical operators. In FORMAT statements, a specific format descriptor is provided for the parsing or formatting of logical values. A common extension prior to FORTRAN 77 was to extend the .EQ. and .NE. operators, or perhaps add a .XOR. operator for comparing Boolean expressions. Fortran 77 added .EQV. and .NEQV. operators to standardize the operations. Fortran 90 added alternative comparison operators <, <=, , /=, >, and >=.

Java

In Java, the value of the boolean data type can only be either true or false.

Unity

In Unity, the value of the boolean can be either true or false and is used in scripting.

Lisp and Scheme

The language Lisp 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 t can be used 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 having 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. Common Lisp, on the other hand, also provides the dedicated boolean type, derived as a specialization of the symbol.

Pascal, Ada, and Haskell

The language Pascal popularized the concept of programmer-defined enumerated types, previously available with different nomenclature in COBOL, FACT and JOVIAL. 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. In contrast, converting 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.

Perl and Lua

has no Boolean data type. Instead, any value can behave as Boolean in Boolean context. The number 0, the strings "0" and "", the empty list , and the special value undef evaluate to false. All else evaluates to true.
Lua has a Boolean data type, but non-Boolean values can also behave as Booleans. The non-value nil evaluates to false, whereas every other data type value evaluates to true. This includes the empty string "" and the number 0, which are very often considered false in other languages.