Boolean datatype
Encyclopedia
|
| Tutorials | Encyclopedia | Dictionary | Directory |
|
Boolean datatype
In computer science, the Boolean datatype, sometimes called the logical datatype, is a primitive datatype having one of two values: true and false, respectively. In some languages the Boolean datatype is defined to represent more than two truth values. For instance the ISO SQL:1999 standard defined a Boolean data type for SQL which could hold three possible values: true, false, unknown (SQL null is treated as equivalent to the unknown truth value, but only for the Boolean data type). This datatype is used in Boolean and other operations such as and (
AdaAda defines The relational operators ( AlgolAlgol 60 had a An actual extract from the ALGOL 68 language specification (page 177) where the boolean operators are defined: 10.2.2. Operations on Boolean Operands
Prior to C99, the standards for the C programming language provided no Boolean type. This however does not mean that C90 cannot represent the concept of boolean values, as all the boolean operators (&&, ||) and conditional statements (if, while) in C interpret nonzero values to signify true and zero values to signify false. Thus, it is common to store boolean values in variables of another type, such as an integer or an enum. For convenience, it is also common to create a typedef for a boolean type, which resolves to some existing datatype. The C99 standard also provides a built-in boolean type. To illustrate Booleans in C, note that the C code: is equivalent to: This is straightforward for integer datatypes. Since C standards mandate that While it is not necessary to name the true and false values in order to test variables for truth or falsehood, it is necessary to do so in order to assign values to them. (One way is to use the values zero and one, which have the advantage of being language-independent.) Alternatively, the The following typical preprocessor macros are also often used. Sometimes However, problems arise from the fact that any non-zero value represents true in C, while the value C99In C99, there is a C++During its standardization process, the C++ programming language introduced the The 1998 C++ Standard Library defines a specialization of the C#In C#, Boolean variables are identified through the reserved word Code to output a Boolean could be represented like this: bool myBool = (i == 5); System.Console.WriteLine(myBool ? "I = 5" : "I != 5"); FortranThe JavaIn the Java programming language, boolean variables are represented by the primitive type The Java Language Specification does not permit any explicit or implicit casts to or from
int i = 1;
if (i)
System.out.println("i is not zero.");
else
System.out.println("i is zero.");
because the integer variable In Java, JavaScriptJavaScript has two keywords
var objBool = new Boolean(false);
if ( false || 0 || "" || null || window.not_a_property ) {
alert("never this");
} else if ( true && [] && {} && objBool ) {
alert("Hello Wikipedia"); // will bring up this message
}
Lambda calculusIn the lambda calculus formal model of computing, booleans are represented as Church booleans. LispLisp has two special symbols MLLike Ocaml, ML has a - fun isittrue x = if x then "YES" else "NO" ; > val isittrue = fn : bool -> string - isittrue true; > val it = "YES" : string - isittrue false; > val it = "NO" : string - isittrue (8=8); > val it = "YES" : string - isittrue (7=5); > val it = "NO" : string Objective-CObjective-C provides a type OcamlOcaml has a Like other enumerated types, a value of this type uses a word of memory. Pascal
Note that values outside the enum are not defined. Some compilers like Delphi have as extension special boolean type that map onto C numeric types for interfacing purposes. (Delphi: bytebool,wordbool,longbool) PerlIn the Perl programming language, there is no distinction between numbers, strings and other non-aggregate data types. (They are all called "scalar".) Aggregate types without any elements, empty strings, numbers which equal a value of 0, the strings Elements of aggregates may also be tested against "existence" or "non-existence"http://perldoc.perl.org/functions/exists.html, and all variables may be evaluated as either "defined" or "undefined".http://perldoc.perl.org/functions/defined.html (An element of a hash or array that has been assigned the value There are no built-in true or false constants in Perl 5, however the values do exist internally in Perl6.
PHPPHP has a boolean datatype [7] with two values: true and false (case doesn't matter). $var = true; $var = false; print $var ? "T" : "F"; print $var == true ? "T" : "F"; print $var === true ? "T" : "F"; print is_bool($var) ? "T" : "F"; print gettype($var); Several values evaluate to a logical false [8] with the loose comparison operator
PHP programmers wishing to distinguish a boolean variable set to false from other types of variable must use the strict comparison operator PythonThe Python programming language defines True and False values as its boolean type, as well as allowing all objects to be tested for their truth value. The following values are considered false:
In all other cases, objects are considered true. Boolean operators and boolean built-in types always return one of the boolean values True and False except for the operators " RubyThe Ruby programming language does not have a Boolean data type as part of the language. Like many other interpreted languages, all variables are dynamically typed. Instead, ruby defines the explicit values of will print "true", which might come as a surprise to a new user of the language. Since Ruby is a pure object-oriented programming language, even the "explicitly" defined values of Would output "FalseClass", "TrueClass" and "NilClass" respectively. SchemeScheme has two special symbols SQLSQL supports three-valued logic (3VL), and comparison predicates in SQL can return any of three possible results: true, false, or unknown. The Boolean datatype was introduced in the ISO SQL:1999 standard, which specified that in addition to the three possible SQL Boolean values, instances of the datatype could be set to null[11]. For DBMSs that implement the ISO SQL:1999 standard, the following code creates a table which holds instances of the Boolean data type. The SQL Boolean data type did not gain widespread adoption, owing to inconsistencies in the standard and lack of support from vendors. Most SQL DBMSs use other data types like bit, byte, and char to simulate the behavior of Boolean data types. Visual BasicIn Visual Basic Boolean values from comparisons can be stored in variables with the Note: Although Boolean values should only be -1 or 0, other values can be coerced into them by calling a function with a XPath and XQueryXML Path Language (XPath 2.0) and XML Query Language (XQuery 1.0) both rely on XML Schema for Boolean data type support. The XML Schema xs:boolean data type supports both true and false Boolean values. XPath and XQuery define a set of rules for calculating the effective Boolean value of expressions. XPath 1.0 and languages based on it, like XML Stylesheet Language (XSL), also support Boolean data types and implicit calculation of effective Boolean values from non-Boolean expressions. See also
Notes and references
Source: Wikipedia | The above article is available under the GNU FDL. | Edit this article
|
|
top
©2008-2009 TutorGig.com. All Rights Reserved. Privacy Statement