Information Technology Topic

Operators and Truth Tables

  1. Read notes
  2. Flashcards
  3. Quiz
  4. Confident

Study focus - 3 things

  1. Arithmetic operators (+ - * /) calculate, relational operators (< > = <= >= <>) compare two values, and logical operators (AND, OR, NOT) combine or reverse TRUE and FALSE results
  2. A comparison or a logical test always gives TRUE or FALSE, and a truth table lists every combination of inputs with the result: a table with n inputs has 2 to the power n rows
  3. AND is TRUE only when both parts are TRUE, OR is TRUE when at least one part is TRUE, NOT flips the value; put brackets round each comparison so the order of working is clear

Section 1Three kinds of operator

An operator is a symbol that tells the computer to do something with values. The values it works on are called operands. In 6 + 2, the operands are 6 and 2 and the operator is +. There are three kinds to know for CSEC IT. Arithmetic operators calculate a number. Relational operators compare two values. Logical operators join or reverse TRUE and FALSE results. Here's the trick: look at what comes out. Arithmetic gives a number, while relational and logical operators give TRUE or FALSE.

Section 2Arithmetic operators and order of working

The four arithmetic operators are + (add), - (subtract), * (multiply) and / (divide). The star is used for multiplication because the letter x would be confused with a variable. The computer does not work strictly left to right. Brackets come first, then * and /, then + and -. So 6 + 2 * 5 is 6 + 10 = 16, not 8 * 5 = 40. Writing (6 + 2) * 5 forces the addition first and gives 40. When operators have the same rank, the working goes left to right. In 20 - 8 / 4 the division is done first (8 / 4 = 2) and then 20 - 2 = 18. In these notes, / always gives the exact quotient, so 7 / 2 is 3.5. In Pascal, / also gives a real (decimal) result even when both numbers are whole numbers, and dividing by zero causes a run-time error.

Section 3Relational operators

A relational operator compares two values and gives TRUE or FALSE. There are six: = (equal to), <> (not equal to), < (less than), <= (less than or equal to), > (greater than) and >= (greater than or equal to). For example, if mark holds 50, then mark > 50 is FALSE, mark >= 50 is TRUE and mark <> 50 is FALSE. Watch out: in a test, = asks a question (are these equal?). Storing a value is a different job. In this course the pseudocode statement is Store 5 in x, and in Pascal the assignment symbol is := (a colon then an equals sign). Do not mix up testing and storing.

Section 4Logical operators

Logical operators work on TRUE and FALSE values, usually the results of comparisons. AND is TRUE only when both parts are TRUE. OR is TRUE when at least one part is TRUE, and that includes both being TRUE. NOT reverses its single value: NOT TRUE is FALSE and NOT FALSE is TRUE. A good test for a bus pass: (age >= 12) AND (age <= 17) is TRUE for 12 to 17 inclusive. A discount day rule: (qty > 10) OR (price < 500) is TRUE if either part holds. Easy to mix up: OR in computing is not 'one or the other but not both'. If both parts are TRUE, the OR is still TRUE.

Section 5Truth tables

A truth table lists every possible combination of TRUE and FALSE for the inputs and shows the result for each. It leaves nothing to chance. With 1 input there are 2 rows, with 2 inputs there are 4 rows, with 3 inputs there are 8 rows. Each extra input doubles the rows, so n inputs give 2 to the power n rows. A AND B: TRUE only in the row TRUE, TRUE. A OR B: FALSE only in the row FALSE, FALSE. NOT A: just two rows, TRUE gives FALSE and FALSE gives TRUE. Here's the trick: list the rows in the same order every time (TRUE TRUE, TRUE FALSE, FALSE TRUE, FALSE FALSE) so none is missed.

Section 6Building bigger conditions

You can join conditions to make a longer one, such as (mark >= 50) AND (attendance >= 80). Each side of AND or OR must be a complete comparison. Writing mark >= 50 AND < 80 is wrong: the second part has no value on its left. Write mark >= 50 AND mark < 80. When a condition has several operators, NOT is worked out first, then AND, then OR. Do not rely on that. Put brackets round every comparison and round any part you want done first, so there is no doubt. To find the result of a compound condition, replace each comparison with TRUE or FALSE and then use the truth tables.

Section 7Operators inside an If

A condition is usually used inside an If-then-else, which picks one path depending on whether the condition is TRUE or FALSE. Read mark Read attendance If (mark >= 50) AND (attendance >= 80) then Display "Pass" Else Display "Repeat" End If If mark is 62 and attendance is 75, then mark >= 50 is TRUE, attendance >= 80 is FALSE, TRUE AND FALSE is FALSE, so the program displays Repeat. Watch out: a student with a mark of 55 and attendance of exactly 80 passes, because >= includes 80.

Examples

A tuck shop in Kingston stores 150 in price and 4 in qty. Find the value of each expression: (a) price * qty (b) (price * qty) >= 500 (c) qty <> 4 (d) price + qty * 10 (e) (price + qty) * 10

(a) 150 * 4 = 600. (b) 600 >= 500 is TRUE. (c) qty is 4, so qty <> 4 asks whether 4 is not equal to 4. That is FALSE. (d) Multiplication first: qty * 10 = 40, then 150 + 40 = 190. (e) Brackets first: 150 + 4 = 154, then 154 * 10 = 1540. Notice (d) and (e) use the same numbers and operators but give different answers because of the order of working. Arithmetic gave 600, 190 and 1540. The two comparisons gave TRUE and FALSE.

A school in Portmore passes a student when A is TRUE (mark >= 50) and B is TRUE (attendance >= 80). (a) Complete a truth table for A AND B, A OR B and A AND (NOT B). (b) Student 1 has mark 62 and attendance 90. Student 2 has 55 and 70. Student 3 has 40 and 95. Find A AND B for each. (c) State which students would satisfy A OR B.

(a) Two inputs give 2 to the power 2 = 4 rows. A, B: A AND B, A OR B, A AND (NOT B) TRUE, TRUE: TRUE, TRUE, FALSE TRUE, FALSE: FALSE, TRUE, TRUE FALSE, TRUE: FALSE, TRUE, FALSE FALSE, FALSE: FALSE, FALSE, FALSE For the last column, NOT B is worked out first. It is TRUE only when B is FALSE, so A AND (NOT B) is TRUE in the second row only. (b) Student 1: A is TRUE and B is TRUE, so A AND B is TRUE. Student 2: A is TRUE, B is FALSE (70 is under 80), so A AND B is FALSE. Student 3: A is FALSE (40 is under 50), B is TRUE, so A AND B is FALSE. (c) A OR B needs only one TRUE. Student 1 has two, Student 2 has A, Student 3 has B. All three satisfy A OR B. Only Student 1 satisfies the strict AND rule.

Now let's water it

Session runs notes to flashcards to quiz back-to-back.

Where this comes from