CS101 Study Guide
Unit 4: Relational and Logical Operators in Java
4a. Explain relational operators such as >,>=, <, <=, ==, and !=
- How are values compared?
- Do relational operators work only with primitives?
- Are arithmetic operators the only way to manipulate data?
- How is Boolean Algebra applied in this language?
Data takes many forms. We have reviewed primitives, classes, and several types of numeric data. There are also structures and booleans. You cannot use most relational operators with classes. The equals relational operator simply checks to see if two variables are pointing to the same memory location. For instance, you cannot apply greater-than or less-than to two boolean variables. So, while it may make sense in the human or numeric way, it is not always something the language can do. Similar rules apply to classes and structures, but not necessarily to their attributes. There are also times when comparisons are performed using a class method rather than a relational operator. Be alert for such cases and use the approach appropriate to the situation at hand.
Review Boolean Expressions: Critical information in these sections: 9, 10, 21
4b. Write Boolean expressions using relational operators
- What is the difference between relational, logical, and boolean operators?
- How can relational and logical operators be combined to create complex comparisons?
- How can boolean operators be used to create extended comparisons?
By combining logical operators and relational operators, you can ask complex questions such as "Is A greater than B at the same time that C is less than D?" While this example seems rather simple, relational operators can be combined with logical operators to make extensive comparisons.
Boolean operators can be used in a similar way to ask such questions as "Are any of these conditions true?" or "Are all of these conditions true?". Of course, boolean operators can be used for other things. An example is the use of bit-shifting to divide an integer by two. In some cases, this can be a lot faster than using the normal division operator. An interesting discussion on this matter can be found here: Should I bit-shift to divide by 2 in Java?
Review Boolean Expressions: See these sections: 3, 7, 8, 11, 14, 15, 16, 19, 20, 22
4c. Use logical operators such as &&, ||, and !
- What is the difference between the && and || operators?
- What does the ! mean in English?
- Can the operators &, &&, ||, |, and ! be combined within the same "if" statement?
- Is an "if" statement the only way to effectively use the operators &, &&, ||, |, and ! ?
- What is the purpose of the ternary, "?", operator?
Logical operators are very powerful, especially when combined with boolean operators since these allow complex questions to be asked programmatically. The more you understand about Boolean Algebra and its associated truth tables, the better you will be able to satisfy sophisticated requirements.
Although not highlighted in the readings, do not ignore Java's ternary, ? relational operator. The ternary operator has three arguments. It is used to combine relational and logical expressions while making decisions on which value should be assigned to the target variable. The basic expression is:
targetVariable = (logical expression) ? value
if expression is true : value
if expression is false;
This is the same as:
if (logical expression) targetVariable = value
when logical expression is true;
else targetVariable = value
when logical expression is false;
Notice that targetVariable value could also be an equation or conditional of one kind or another.
It is certainly not the case that an if statement is the only way to get good value from relational, logical, and boolean operations. variableX = operationY;
is also useful, if variableX
is suitable for receiving the results of operationY
. Consider the full range
of options available to you. A Quick Intro to Java, section 2.10, pg. 23 on "targetVariable value" explores this niche topic further.
Review Fill in the Blank: Operators: This review does a good job of bringing together the several lessons on Learning Outcomes 4a, 4b, and 4c.
4d. Evaluate truth-tables
- Why are truth tables important?
- What are the implications of De Morgan's Laws?
- What is the "negation" of AND and OR?
- What is the negation of a negation?
- What is the precedence of negation?
There are two tools presented here that are important as your programs become necessarily more sophisticated and complex. (Yet, do not think making your programs look complicated is the way to career success. It is not.) Truth tables are used to organize relational and logical expressions to facilitate their expansion. De Morgan's rules help us reduce the complexity of a given expression so that it is easier to understand its implications and to implement the reduced expression in circuitry and computer code.
An example truth table:
A |
B |
A AND B (A && B) |
A OR B (A || B) |
false |
false |
false |
false |
true |
false |
false |
true |
false |
true |
false |
true |
true |
true |
true |
true |
Note the independence of the logical expressions A and B. Logical expressions are evaluated independently. Relational operators make use of those results. The result is boolean, either true or false.
According to De Morgan, !(A && B) = (A&&B) = !A || !B. He also states that !(A || B) = (A || B) = (!A && !B). These are good to know. You can legitimately restate the specification as long as the requirement is met. Restated specifications are sometimes easier to meet. Although the readings do not discuss the matter, binary circuitry (the type used with computers and embedded computational devices) can often be made less expensive through the use of De Morgan's Rules. This kind of thing is called "logical equivalence". You can view a good eight-minute video on this topic (Logical Equivalence). It is part of a series should you want to dig deeper than the readings take you.
Review Truth Tables and De Morgan's Law: Be particularly familiar with sections 2, 4, 7, 8, 9, 10, 11, 12, and 18; the rest are for practice.
Unit 4 Vocabulary
This vocabulary list includes the terms listed above that you will need to know to successfully complete the final exam.
- bit-shifting
- De Morgan's rules
- logical operators
- relational operators
- ternary operator
- truth table
XOR
AND
NOT
OR