Eng:Truth Table

A truth table is a table which displays the output of a Boolean logic circuit for various inputs.

Uses
Truth tables are used by circuit designers to predict the evaluation of various logic circuits. Logic circuits are simplified by circuit designers into a circuit of just NAND gates, which can be represented electronically with transistors. So the complex circuit can be converted into a physical circuit. This is how computer processors work, on a low level.

Operators
The following is a list of logic operators, or gates.

AND
The AND operator is very simple. It returns true if both conditions are true:

NAND
The NAND operator is the opposite of the AND operator, only returning false when both conditions are true. NAND means "Not AND".

There is no block for it in Scratch, but its name suggests its workaround: >

OR
The OR operator is very simple. It returns true if either or both conditions are true:

It differs from the AND operator when one value is true and one is false. AND returns false, while OR returns true.

NOR
The NOR operator is opposite of the OR operator, only returning true when both conditions are false. NOR means "Not OR".

There is no block for it in Scratch, but its name suggests its workaround: >

XOR
The XOR operator returns true if either, but not both conditions are true. XOR stands for "exclusive OR".

It is not available with a block in Scratch, but can be simulated with the following script: >

XNOR
The XNOR operator returns false if either, but not both conditions are true. XNOR stands for "exclusive NOR". It is the opposite of the XOR operation.

It is not available with a block in Scratch, but can be simulated with the following script: <(condition 1) = (condition 2)>

It is sometimes called XAND.

NOT
NOT is different from the other operators in that it only takes one parameter. It inverts the value.

Common Symbols
Many high and low level programming languages use a set of common symbols which represent logic functions. (Note that the single and double versions of & have slightly different meanings. Similarly for .)

In Python and some other languages, the words and, or, and not are used directly.

Combining Logic Operations
Logic operations are much more useful if combined. For example, T||(T&&F) returns true. By combining logic circuits, one can create different truth tables. One quick way to generate these, as well as a display of schematic diagram of the circuit, is WolframAlpha, a computation engine. An example can be found ||!%28A%26%26B%29%29 here.

Short-Circuit Evaluation
Generally, the better method for evaluating booleans is something called short-circuit evaluation. This means that arguments should be evaluated one by one rather than all at once. For example, consider (1=0)&&(0/0 = 1). Here, conventional (eager) evaluation would result in an error since it would try to evaluate 0/0. However, with short circuit evaluation, it will first evaluate (1=0) and see it is false. Since && returns true only if all values are true, it automatically returns false without evaluating the second argument.

Scratch uses eager evaluation. This can be tested with this script: <<[1] = [0]> and <((0) / (0)) = [0]>>

which returns an error in Scratch. There is a rather simple workaround for short-circuit AND evaluation though: set [T/F v] to [false] if if  set [T/F v] to [true] end end For OR: set [T/F v] to [false] if set [T/F v] to [true] else if   set [T/F v] to [true] end end

Snap!, however, uses short-circuit evaluation, as shown:



Also, a multiple-input AND that uses short-circuit can be created as shown: