Lesson 04: Comparisons
what do you think comparative operators/conditionals are?
Here are some examples of operators that can help a programmer compare:
how could you use conditionals/comparatives when programming?
why do you think programmers use comparative operators?
Logic Gates
Complete the following sentence below:
"When you go to the shops, can you buy some cereal ____ some bread?"
What word did you use?
How did it affect the sentence's meaning?
Checking multiple statements at once
Some solutions to the previous question could have been :
- … buy some cereal AND some bread
- … buy some cereal OR some bread
- … buy some cereal NOT some bread
You can see that by adding in a single word changes the statements outcome.
Basic logic gates
Programs need to know what to output depending on the input given. In order for programs to check multiple inputs at once, we can use the basics of logic gates.
Think of a logic gate as a switch and your code as a circuit. The switch determines if that section of the circuit should be powered on or off. These switches can have different conditions.
AND Gate
The first and one of the simplest logic gates is the AND gate. This checks if 2 or more inputs are all TRUE. If even one input is false, the gate does not allow the code to flow.
IF input A AND input B are true. Then output Y is True
OR Gate
The second is an OR gate. This allows the flow of code if ANY of the inputs are TRUE.
IF input A OR Input B is true. Then Output Y is true
NOT Gate
This one behaves slightly differently. It instead outputs the opposite of the signal that it receives.
IF Input A is true, Output Y is false IF Input A is false, Output Y is true
Logic gates in our code are generally used when we should or shouldn't have something occur. Sound familiar?
IF Statements are the best example of this. But how do we write these logic gates in our code?
Solving Truth Tables
Analyse the logic circuit below and determine all of the different outcomes. Output your results in a truth table. The first couple have been done for you.
Kua mutu? Finished?
Try constructing your own logic circuits by visiting logic.ly here!