# 1 4. Computer Maths and Logic 4.2 Boolean Logic. 4.2.1 Boolean Operators.

## Presentation on theme: "1 4. Computer Maths and Logic 4.2 Boolean Logic. 4.2.1 Boolean Operators."— Presentation transcript:

1 4. Computer Maths and Logic 4.2 Boolean Logic

4.2.1 Boolean Operators

3 Boolean logic This is concerned only with the values true (1) and false (0). In the statement “if it is warm and not raining then we will go for a picnic” you can substitute all 3 parts with true or false, depending on the conditions.

4 Boolean operators True/false statements can be worked on using the following boolean operators: ‣ AND ‣ OR ‣ NOT ‣ XOR

5 Boolean operators Usually, you are combining 2 or more statements (the inputs) into one result (the output). Note the similarity with normal algebra, e.g. ‣ 3 x 4 = 12 ‣ where the x operator combines two inputs into one output.

6 AND Both inputs must be true for the output to be true. Symbol: e.g. A B = C So, 'if it is warm and sunny then we will go for a picnic' becomes ‣ 'If (it is warm it is sunny) = go for a picnic' ‣ A B = C where A = sunny, B = warm, C = picnic Note that it has got to be both.

7 AND in Java Use two ampersands (&&): ‣ boolean sunny, warm, goToBeach; if (sunny && warm) { goToBeach = true; } else { goToBeach = false; }

8 OR Only one input need be true for the output to be true. Symbol: + e.g. A + B = C So, ‘if it is warm or it is sunny then we’ll go for a picnic' becomes ‣ if (it is warm + it is sunny) = go for a picnic' ‣ A + B = C where A = sunny, B = warm, C = picnic This means we are not so fussy (warm and cloudy OK, cool and sunny OK, but not cool and cloudy).

9 OR in Java Use two break bars (||): ‣ boolean sunny, warm, goToBeach; if (sunny || warm) { goToBeach = true; } else { goToBeach = false; }

10 NOT This simply reverses the truth of the input. Symbol: o ̅̅ v ̅ e ̅ r ̅ l ̅ i ̅ n ̅ e ̅ e.g. A B ̅ = C So, ‘if it’s warm and it’s not raining then we’ll go for a picnic' becomes ‣ if (it’s warm it ̅ ’ ̅ s ̅ ̅ r ̅ a ̅ i ̅ n ̅ i ̅ n ̅ g ̅ ) = go for a picnic' ‣ A B ̅ = C where A = warm, B = rain, C = picnic This means it must be warm and it must NOT be raining.

11 NOT in Java Use the exclamation mark (!) boolean sunny, warm; if (!warm) { if (temp != 0) { output(“Good for mountain biking."); } }

12 XOR The exclusive OR - one or the other but not both. Symbol: ⊕ e.g A ⊕ B = C. So, ‘if it’s warm or sunny but not both, we’ll go for a picnic’ becomes ‣ ‘if (it’s warm ⊕ it’s sunny) = we’ll go for a picnic’ ‣ A ⊕ B = C where A = warm, B = sunny, C = picnic This means warm and cloudy OK, cool and sunny OK, but not cool and cloudy and not warm and sunny.

13 NAND and NOR The equivalent of doing AND / OR then reversing the result. Symbols: ̅ e.g. A ̅ B = C and + ̅ e.g. A + ̅ B = C. ‣ ‘If it is not (warm and sunny) then we are going for a picnic.’ ‣ ‘If it is not (warm or sunny) then we are going for a picnic.’ Can these be said another way?

Download ppt "1 4. Computer Maths and Logic 4.2 Boolean Logic. 4.2.1 Boolean Operators."

Similar presentations