Boolean Expressions with Mixed AND and OR

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   hear noise   go to next page

Answer:

Yes.

Boolean Expressions with Mixed AND and OR

cash is 0;credit is 26000; debt is 500. The boolean expression is evaluated like this:

cash >= 25000  ||  ( credit >= 25000 && debt < 1000 )

   false       ||  ( credit >= 25000 && debt < 1000 )

   false       ||  (      true       && debt < 1000 )
   
   false       ||  (      true       &&    true     )

   false       ||  (                true            )

              true

Parentheses are used to group the two relational expressions that are to be ANDed. (Since && has higher precedence than ||, the parentheses are not needed, but they don't hurt.) The following expression is not equivalent:

( cash >= 25000  ||  credit >= 25000 ) && debt < 1000 

When boolean expressions contain both && and || correct grouping is important.

QUESTION 14:

Use the (incorrect) boolean expression above to answer this question: You have $50,000 in cash, $100,000 of credit, and $3,000 of debt. Can you buy the car?