230 likes | 436 Views
Boolean Algebra. 25 March 2014. Boolean Logic. Invented by George Boole Boolean logic is the basis for modern computer logic. What is a Boolean Variable?. A variable (cell) that holds either true or false Can be thought of conceptually as yes or no. Boolean Expressions: Comparisons.
E N D
Boolean Algebra 25 March 2014
Boolean Logic • Invented by George Boole • Boolean logic is the basis for modern computer logic.
What is a Boolean Variable? • A variable (cell) that holds either true or false • Can be thought of conceptually as yes or no
Boolean Expressions: Comparisons • Less than (<) • Greater than (>) • Equal to (=) • Less than or equal to (<=) • Greater than or equal to (>=) • Not equal to (!=)
Boolean Operations • In Math, there are operations: plus, minus, times, etc. • In Boolean Algebra, there are operations as well. • Boolean Operations: • AND • OR • NOT
AND • If every boolean being “and-ed” is true, then the AND operator returns true. • true AND true AND true = true • true AND false AND true = false • Did all students pass the course?
OR • If some boolean being “or-ed” is true, then the OR operator returns true • false OR false OR false = false • false OR false OR true = true • Did at least one student pass?
NOT • NOT(one argument) • If p is true, then NOT p is false. • If p is false, then NOT p is true.
NOT • NOT (circles) • blue & white • NOT(diagrams) • yellow & white • NOT(circles OR diagrams) • white • NOT(circles AND diagrams) • yellow, blue, white • Did neither student pass the course?
Properties • Just as in Math, the operations in Boolean Algebra follow certain properties.
De Morgan’s Laws • NOT (P AND Q) = (NOT P) OR (NOT Q) • NOT (P OR Q) = (NOT P) AND (NOT Q)
De Morgan’s Law (1) • The dog is not both quiet and small • Either the dog is not quiet or it is not small • NOT (QUIET AND SMALL) • (NOT QUIET) OR (NOT SMALL) NOT (A AND B) = (NOT A) OR (NOT B)
De Morgan’s Law (2) • The cat is not black or white • The cat is not black and it is not white • NOT (BLACK OR WHITE) • (NOT BLACK) AND (NOT WHITE) NOT (A OR B) = (NOT A) AND (NOT B)
IF … THEN … ELSE • if (MUST BE LOWERCASE) • No else: do nothing if not true • else: one or the other • else if: nested function
Operators Logical Conditional
N-way Decisions • What if I want to decide between more than 2 alternates? • Nested else statements (else if) • switch – case
Random Selection Condition is random function var n = Math.random(); • Choose between options
Converting Random to Integer • Often useful to convert that random number to an integer • 0->1 needs to be changed to 0->3 (or any other number • varnInt = n*4; gets the range correct • But only want integer only: Math.floor returns the largest integer less than the value • varnInt = Math.floor(n*4);
Changing Other Attributes • Instead of changing value, can change image
Changing in the function • If you have two things that you want to change in your HTML Can change them in the function • Usually do NOT return value