Boolean Expressions

This chapter introduces relational and logical operators. It also discusses the use of these operators to write Boolean expressions.

17. Car Purchase Decision



Car Purchase Decision

You need money OR credit. Just one would be enough. Of course, if you had lots of money and plenty of credit you could certainly buy the car.

Sometimes a program has to test if one or the other (or both) of the conditions has been met. Here is how that is done with the car purchase problem.

How much cash do you have?    

How much credit do you have?  

  if ( cash>=25000 ||  credit>=25000 )
    System.out.println("Enough to buy this car!");
    System.out.println("Have you considered a Yugo?");


The symbol || (vertical-bar vertical-bar) means OR. On your keyboard, vertical-bar is the top character on the key above the "enter" key. The OR operator evaluates to true when either qualification is met or when both are met. The if statement asks a question with two parts:

if ( cash >= 25000 ||  credit >= 25000  )
------------- -------------
cash part credit part

If either part is true, or both parts are true, then the entire boolean expression is true.

Question 17:

Say that you enter 56000 for cash and 0 for credit. What answer (true or false) does each part give you?

    cash >= 25000 credit >= 25000

What does the entire boolean expression give you?

    cash >= 25000 || credit >= 25000