Equivalent Relational Expressions

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

if ( value == 399 )
  System.out.println("Correct Value");
else
  System.out.println("Wrong Value");

Equivalent Relational Expressions

In the following, X and Y represent numbers that can be compared.

ComparisonEquivalent ComparisonEquivalent
!(X < Y)X >= Y   !(X >= Y) X < Y
!(X > Y)X <= Y   !(X <= Y) X > Y
!(X == Y)X != Y   !(X != Y) X == Y

Usually if you have a comparison that uses a NOT operator, replace it with an equivalent comparison that does not use a NOT.

QUESTION 12:

Rewrite the following if statement:

if ( !(car.price > 8000 ) )
  System.out.println("Affordable");
else
  System.out.println("Too Expensive!");