Page 173 - Introduction to Programming with Java: A Problem Solving Approach
P. 173

                4.15 Problem Solving with Boolean Logic (Optional) 139 Running the modified program produces the following sample session:
Sample session:
GARAGE DOOR OPENER SIMULATOR
Press Enter, or enter 'q' to quit:
moving up
Press Enter, or enter 'q' to quit: stop
Invalid entry.
Press Enter, or enter 'q':
stopped
Press Enter, or enter 'q' to quit: q
Optional Forward References
invalidentry
corrected entry
       At this point, some readers might want to learn about arrays. An array is a collection of related items of the same type. Array manipulations require the use of loops. As such, arrays provide a means for readers to gain further practice with the material presented in Chapter 4, specifically the loop material. You’re not required to learn about arrays just yet, but if you can’t wait, you can read about arrays in Chapter 10, Sections 10.1 through 10.6.
Later in the book, we present relatively advanced syntax details that pertain to control statements. For example, embedding an assignment expression in a loop header or using a break statement to break out of a loop. You’re not required to learn those details just yet, but if you can’t wait, you can read about them in Chapter 11, Sections 11.6 tAhrpougahg11.o12. PDF Enhancer
4.15 Problem Solving with Boolean Logic (Optional)
The conditions for if statements and loops can sometimes get complicated. For a better understanding of complicated conditions, we’ll now look at the logic that comprises a con- dition. Learning how to manipulate logic should help you to (1) simplify condition code and (2) debug logical problems. What we’ll be talking about is known as Boolean logic or Boolean algebra.
Make the logic as clean as possible.
                       The building blocks for Boolean logic are things that you’ve already seen—the logical operators &&, ||, and !. You’ve seen how the logical operators work when applied to comparison-operator conditions. For example, this code (which uses the && operator in conjunction with the >= and <= comparison operators) probably already makes sense to you:
(temp >= 50.0 && temp <= 90.0)
Boolean Algebra Basic Identities
Sometimes, however, a logical expression is harder to understand. This is particularly true when it includes several “not” (!) operators. To gain a better understanding of what the code means and is supposed to do, it’s sometimes helpful to transform the logical expression to another form. Boolean algebra provides a special set of formulas called basic identities, which anyone can use to make transformations. These basic identities are listed in Figure 4.20. The precedence of the various operators is the precedence given in Figure 4.6. That is, ! has highest precedence, && has next highest precedence, || has the lowest precedence. The ←→ symbol means equivalence; that is, whatever is on the left side of the double arrow can be replaced by whatever is on the right side, and vice versa.













































































   171   172   173   174   175