Information Science Boolean Expressions Week College of Information Science and Engineering Ritsumeikan University
Topics covered l Terms and concepts from Week 9 l Binary (Boolean) logic History Boolean data type Relational and logical operators Boolean and algebraic expressions Operator precedence Truth tables and De Morgan s Law l Test 2
Recall Week 9 l White space l Line and block comment statements l Operator, Operand l (Arithmetic) expression l Integer division, Modulus l Mixed-type arithmetic l Implicit and explicit type casting l Shortcut operators l Operator precedence 3
Our goals today l To learn about Boolean Logic l To discuss relational and logical operators and how they are evaluated in expressions l To learn how to construct truth tables l To understand De Morgan s laws 4
The problem l Our daily life is based on many binary decision-making cognitive processes, which can often be represented as an algorithm with basic selection statements: Is condition true? No action A Yes action B l Many other decisions, however, require choosing from many alternatives 5
History: George Boole l Boolean logic is binary because it uses only two logical values: True ( or On or Yes ) False ( or Off or No ) l In 854, George Boole published his work An Investigation into the Laws of thought, where he described a system for symbolic and logical reasoning l This system was later called Boolean logic l Presently, Boolean logic is the basis for computer and program design 6
Boolean expressions and Boolean algebra l A Boolean expression has the form of a condition, <condition> l A Boolean expression always evaluates to either true or false l Boolean expressions are written using boolean-type operands and relational and logical operators l Boolean algebra provides the operations and the rules for working with Boolean variables and constants 7
Boolean variables and constants l Let B = {, }, where stands for (is equal to) FALSE and stands for TRUE. The variable x is called a Boolean variable if it assumes values only from B l In programs, we define Boolean variables (constants, fields, etc) the same way as other variables: boolean hasmiddlename; const boolean isflammable = TRUE; l The reserved (key-)words are: boolean (or bool), TRUE, and FALSE 8
Relational operators l Boolean expressions compute Boolean-type results and make use of the relational operators: < less than <= less than or equal to > greater than >= greater than or equal to == equal to!= not equal to 9
Examples of Boolean (relational) expressions (-5 < 5) (4 <= -5) (3 > 3) (3 >= 3) (7 == ) (2!= 7) (5 = ) True False False True False True Error! Not a Boolean expression
Examples of assignments with Boolean expressions boolean b, b2, b3, b4, b5, b6; b = ( 4 < 5); // b is assigned TRUE b2 = ( 4 <= 3); // b2 is assigned FALSE b3 = ( 3 > 3); // b3 is assigned FALSE b4 = ( 4 < (3 + 2)); // b4 is assigned TRUE int x = 7; // and these are algebraic expressions b5 = ((x 5) <= 3); // b5 is assigned TRUE b6 = b5 + ; /* b6 is, in most programming languages, assigned TRUE */ l FALSE is usually associated with the value (zero), while TRUE with a non-zero value (e.g., -, 2, etc)
l Logical operators Besides relational operators, Boolean expressions also make use of the logical operators: The negation (logical NOT) is denoted by the symbol! or or. It is defined by!false == TRUE and!true == FALSE The Boolean sum (disjunction, logical OR) is denoted by or or and is defined by TRUE TRUE == TRUE, FALSE FALSE == FALSE, TRUE FALSE == TRUE, FALSE TRUE == TRUE The Boolean product (conjunction, logical AND) is denoted && or & or and is defined by TRUE && TRUE == TRUE, FALSE && FALSE == FALSE, TRUE && FALSE == FALSE, FALSE && TRUE == FALSE 2
3 AND and OR A B A && B FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE A B A B FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE
Boolean arithmetic l An easy way to evaluate Boolean (logical) expressions is to replace: TRUE with, and FALSE with, with +, && with *,! with NOT l Next step is then just to do the arithmetic: OR + + + AND * * * NOT() NOT() + * 4
5 Boolean operations and circuits x y x y z z l AND: z = x & y l OR: z = x y x z l NOT: z = ~x
6 Algebraic expressions: Operator precedence (postfix) ++, -- st (prefix) ++, --,!, NOT 2nd *, /, % 3rd +, - 4th <, >, <=, >= 5th ==,!= 6th &&, AND 7th, OR 8th (assign) =, +=, *=, etc. 9th
Truth tables B l A truth table can be used to show the relationships between the input and the output of an algorithm A C consider an alarm which sounds when: A. The lights are on and B. The door is open and C. The key is not in the ignition Alarm system P = A && B &&!C P Buzzzzz!!! Truth table: A B C P 7
Truth tables: Another x y z F(x, y, z) example l Problem: For the given truth table, construct the corresponding boolean expression l Solution: Since F(x, y, z) = if and only if (x == y == z == ) or (x == y ==, z == ) or (x ==, y == z == ), F(x, y, z) =!x &&!y &&!z!x &&!y && z x &&!y &&!z 8
9 De Morgan's Laws l Expressions with logical operators &&,, and! obey De Morgan's laws: l Example! (p && q) == (!p!q ) and! (p q) == (!p &&!q ) if (! ( x => - && x <= ) ) Now, let s make it simpler / easier to read: if ( x < - x > )
Other laws of Boolean logic (x ) == x and (x && ) == x Identity laws (x (!x)) == and (x && (!x)) == Domination laws ((x y) z) == (x (y z)) and Associative ((x && y) && z) == (x && (y && z)) laws (x y) == (y x) and Commutative (x && y) == (y && x) laws (x (y && z)) == ((x y) && (x z)) Distributive and laws (x && (y z)) == ((x && y) (x && z)) 2
2 Concepts you need to know after this class l Boolean logic and Boolean algebra l Boolean and algebraic expressions l Relational operators l Logical operators l Operator precedence l Truth tables l De Morgan's laws
22 l Read these slides again l Do the self-preparation assignments l Learn the vocabulary Homework l Consult, whenever necessary, the textbook materials
23 Next class l Fundamental Programming Constructs Basic syntax of high level languages Conditional & iterative control structures
Test 3 24