AND Operator

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:

How much flour do you have? 6
How much sugar do you have? 4
Enough for cookies!

When execution gets to the if statement, it finds that

flour >= 4    — is true, because 6 >= 4

and

sugar >= 2    — is true, because 4 >= 2

Both sides are true, so AND gives true.

AND Operator

The and operator requires that both sides are true:

this side must be true && this side must be true

If both sides are true, the entire expression is true. If either side (or both) are false, the entire expression is false. && is a logical operator because it combines two true/false values into a single true/false value.

and operator in action

Here is what && does:

  • true  && true  = true
  • false && true  = false
  • true  && false = false
  • false && false = false

Use and when every requirement must be met.

QUESTION 5:

Look at the boolean expression:

flour >= 4 && sugar >= 2 

What will the expression give us if flour is 2 and sugar is 0?