Truth tables are used to represent all the inputs/output situations of a section of code in a easily readable and time saving orientation. Here is a simple two input / one output truth table for an A AND B condition.
A very explicit example of a truth table to represent A&&B
A very explicit example of a truth table to represent A&&B
boolA | boolB | (boolA&&boolB) |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
A more simplified version of the same thing. Usually this is written to save time.
A | B | (A&&B) |
---|---|---|
T | T | T |
T | F | F |
F | T | F |
F | F | F |
Other ways of writing true/false in truth tables are: High / Low H / L On / Off 1 / 0 X / O
Five Important Things to Note
- All combinations of the inputs, A and B are included in the A&&B truth table.
- The columns are all headed by the object (variable) they describe.
- The output is dependent on the input values.
- There are only two outcomes (T, F) for four different input combinations (TT, TF, FT, FF)
- The only values a box can exist are boolean (logic) values, true and false.
Truth tables for an AND condition are not used because most people already know the outcome of an AND condition, but in real life conditions are a lot more complex and involved. A step up from the AND truth table completed above would be a truth table with a third input variable, which I will call C. If we want to create a table of values for (A&&B)||C which in plain English just means both A and B are true and/or C is true.
Refer to Java Logical Operator Syntax if you did not know that && symbolized the AND operation, or that the || symbolized the OR operation.
Refer to Java Logical Operator Syntax if you did not know that && symbolized the AND operation, or that the || symbolized the OR operation.
A | B | C | ((A&&B)||C) |
---|---|---|---|
T | T | T | True |
T | T | F | True |
T | F | T | True |
T | F | F | False |
F | T | T | True |
F | T | F | False |
F | F | T | True |
F | F | F | False |
Java Code:
if((A&&B)||C){
//actions to occur when the condition evaluates to true
} else {
//actions to occur when condition is false
}
Although the condition (A&&B)||C can be figured out mentally (with some practice), it is nearly impossible to do that for conditions that look like this: ((A||B) || (C&&!D)). The condition ((A||B) || (C&&!D)) is too complex to be done mentally, so we need to break the condition into parts. If you make the output (final column) a function based on two inputs, the outputs of bool1=(A||B) and bool2=(C&&!D) then the large statement can be a simple output = (bool1||bool2). The sacrifice for making the equation simplified is only an extra two columns!
if((A&&B)||C){
//actions to occur when the condition evaluates to true
} else {
//actions to occur when condition is false
}
Breaking Large Conditions Into Parts
Although the condition (A&&B)||C can be figured out mentally (with some practice), it is nearly impossible to do that for conditions that look like this: ((A||B) || (C&&!D)). The condition ((A||B) || (C&&!D)) is too complex to be done mentally, so we need to break the condition into parts. If you make the output (final column) a function based on two inputs, the outputs of bool1=(A||B) and bool2=(C&&!D) then the large statement can be a simple output = (bool1||bool2). The sacrifice for making the equation simplified is only an extra two columns!
# | A | B | C | D | bool1 = A||B | bool2 = C&&!D | bool1 || bool2 |
---|---|---|---|---|---|---|---|
1. | T | T | T | T | True | False | True |
2. | T | T | T | F | True | True | True |
3. | T | T | F | T | True | False | True |
4. | T | T | F | F | True | False | True |
5. | T | F | T | T | True | False | True |
6. | T | F | T | F | True | True | True |
7. | T | F | F | T | True | False | True |
8. | T | F | F | F | True | False | True |
9. | F | T | T | T | True | False | True |
10. | F | T | T | F | True | True | True |
11. | F | T | F | T | True | False | True |
12. | F | T | F | F | True | False | True |
13. | F | F | T | T | False | False | False |
14. | F | F | T | F | False | True | True |
15. | F | F | F | T | False | False | False |
16. | F | F | F | F | False | False | False |
The java code that would accompany such a Truth table (TT) would look like so:
if((A||B)||(C&&!D)){
//true cases
} else {
//false cases
}
if(reader.isHappy()){ reader.follow("W7R"); reader.findOnYoutube("W7R"); reader.tellFriends("Conditionals are easy!"); }
No comments:
Post a Comment