Truth Tables and De Morgan's Rules
Read this chapter, which discusses Boolean variables as used in decision tables. Truth tables are used to collect variables and their values relative to decisions that have to be made within control structures.
12. Equivalent Relational Expressions
Answer:
if ( value == 399 ) System.out.println("Correct Value"); else System.out.println("Wrong Value");
Equivalent Relational Expressions
Usually if you have an expression that uses a NOT operator, replace it with an equivalent comparison that does not use a NOT. If this is not possible, rewrite the expression so that the NOT applies to the smallest subexpression possible.
In the following, X
and Y
represent numbers that can be compared.
Expression | Equivalent | Expression | Equivalent | |
---|---|---|---|---|
!(X < Y) | X >= Y | !(X >= Y) | X < Y | |
!(X > Y) | X <= Y | !(X <= Y) | X > Y | |
!(X == Y) | X != Y | !(X != Y) | X == Y |
Question 12:
Rewrite the following
if
statement:if ( !(car.price > 8000 ) ) System.out.println("Affordable"); else System.out.println("Too Expensive!");