CIUC- Relational and Boolean Logic Operators Lesson
Relational and Boolean Logic Operators
You learned in the last module that relational operators are used to show comparison. You used relational operators to form boolean expressions used with for and while statements to start and stop a block of code in iteration.
for(int i = 0; i < 20; i++) The boolean expression is i < 20
Relational Operators are also used with conditionals. A conditional structure is a decision structure or selection statement. It consists of an if statement that executes a certain block of code based on if the condition is true or false.
Below are examples of boolean expressions used to form comparisons:
if(mouseX < 250)
if(mouseX < width/2)
Sometimes two or more conditions need to be checked.
if (mouseX < width/2) and (mouseY < height/2)
if (mouseX >= width/2) or (mouseY >= height/2)
When you have two conditions that need to be checked, you must use a boolean logic operator.
What is a Boolean Logic?
Boolean logic is a method for telling if an expression is true or false. Computers use boolean logic to make decisions. Hardware inside the computer consists of logic gates. These logic gates will return a true or false value based on the inputs. In boolean logic, true is represented by the binary digit 1 and false by the binary digit 0. The value returned depends on whether the input is a 1 or 0 and the type of logic gate it goes through.
There are three main Boolean Logic Operators used in programming.
Java Operator | What the Operator Returns |
---|---|
! | not - changes the logic value |
&& | and - returns true if both parts are true and false |
|| | or - returns true if one or both parts are true and false only if both parts are false |
AND Operator:
An AND operator && should be used when you need both conditions to be true. It will only return true when both conditions are true. When one is false, it will return false.
OR Operator:
An OR operator || is used when only one of the conditions must be true for the condition to execute.
Not Operator:
A NOT operator ! is used to negate or change the output to the opposite of the input. It only has one input. If the input is true, the output is false. If the input is false, the output is true.
Each of the logic gates is represented by a symbol. A truth table is used to show the output resulting from all the possible inputs for each type of logic gate.
The truth table and symbol for each logic gate is shown below.
What is Short Circuit?
If the first part of the logic statement is enough to determine true or false for the entire statement, then the second part isn't evaluated.
In an AND (&&) statement this is when the first part is false. Since both conditions need to be true, is the first one is false the output will short circuit to false.
((5==6) && (6==6))
false && short circuit
This will short circuit and return false. The second condition will not be evaluated because it has already determined the answer is false since both conditions need to be true.
For an OR (||) statement it happens when the first part is true. Since only one condition must be true, it will short circuit when the first one is true.
Order of Precedence with Boolean Operators
Boolean Logic Expressions Activity
When evaluating Boolean expressions, you need to follow the order of precedence for operators.
Evaluating Boolean Expressions Activity
Complete the learning activity on Evaluating Boolean Expressions below.
[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION