Java

TOPIC 32 – BOOLEAN FUNCTIONS

 

 

LESSON NOTE

 

 

DEFINITION

 

A Boolean function is simply a function that returns either true or false.

 

EXAMPLE

 

The function below returns true whenever the provided parameter is larger than 100.  Otherwise, it returns false.

 

public static boolean isBiggerThanAHundred(int x)

{

   if (x > 100)

   {

      return true;

   }

   else

   {

      return false;

   }

}

 

BOOLEAN FUNCTION CALL AS CONDITION

 

Remember that a condition is simply an expression that evaluates to either true or false.  Therefore, a Boolean function call can be used as a condition.

 

EXAMPLE

 

The Boolean function from above is being used as a Boolean expression inside the if statement below:

 

if (isBiggerThanAHundred(mark))

{

  

}

 

HIGHER LEVEL CODING

 

Boolean functions often allow programmers to think about programs at a higher level by hiding some of the details inside functions.  The function names can also be chosen to make the program seem like English.

 

EXAMPLE – HIGHER LEVEL THINKING

if (gameIsOver())

{

   if (newNewHighScore(score))
   {

       displayHighScoreScreen();

   }

   displayRestartScreen();

}