Selasa, 06 Januari 2015

Boolean Data Types



Boolean explanation

In computer science, the Boolean data type is a data type, having two values (usually denoted true and false), 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 primarily associated with conditional statements, which allow different actions and change 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 have to be Boolean.

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.[1][2] 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.

Most programming languages, even those that do not have an explicit Boolean type, have support for Boolean algebraic operations such as conjunction (AND, &, *), disjunction (OR, |, +), equivalence (EQV, =, ==), exclusive or/non-equivalence (XOR, NEQV, ^, !=), and negation (NOT, ~, !).

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 (introduced in SQL:1999) 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.


Boolean Examples

Below is an example chart that help explains the Boolean operations even more by giving examples and explanations of each of the different Boolean situations.

Example data = "Rull's computer is a location where users can find free computer help and information"


Boolean Value_1 Value_2 Explanation Results
AND Free Help Because the above example data contains both "free" and "help" the results would be TRUE. TRUE
AND Expensive Help Although the above example does have "help", it does not contain "expensive," which makes the result FALSE. FALSE
OR Free Help The above example data has both "free" and "help" but the OR Boolean only requires one or the other, which makes this TRUE. TRUE
OR Expensive Help Although "expensive" is not in the example data, it still does contain "help," which makes the TRUE. TRUE
NOT Free The above example does contain "free", which makes the result FALSE. FALSE
NOT Expensive The above example does not contain "expensive," which makes the result TRUE. TRUE
XOR Free Help The above example contains both "free" and "help", the XOR Boolean only requires one or the other, but not both, making this FALSE. FALSE
XOR Expensive Help The above example does not contain "expensive" but does contain "help," which makes this TRUE. TRUE

Boolean in programming

When programming, a boolean can be done with conditional statements (i.e. if statement) as shown in the example below using Perl.

use strict;
my ($name, $password);
print "\nName: ";
chomp($name = <STDIN>);
print "\nPassword: ";
chomp($password = <STDIN>);
if (($name eq "rull") && ($password eq "pirates")) {
print "Success\n";
} else {
print "Fail\n";
die;
}

In the above example, the if statement is looking for a username that is equal to "rull" and a password that is equal to "pirates". If either the name or password is not correct the program will print "Fail" and die.

1 komentar:

  1. You can clear many of your doubts regarding boolean data types in Core Java through Merit Campus, visit: http://java.meritcampus.com/core-java-topics/data-types-in-java, http://java.meritcampus.com/core-java-topics/boolean-data-type-in-java

    Not only data types, we also have each and every topic in Core Java with example for each. You can read lot of sessions and can write many practice tests in Merit Campus Java website. visit: http://java.meritcampus.com/core-java-topics/ to know more.

    BalasHapus