Simple Logical Examples
Now, the next thing to do is to demonstrate some examples of the usage of the logical operators. Here are a couple of examples that will help you understand the usage of these operators.
Consider the following conditions related to a student.
boolean tall = true;
boolean athleticallyInclined = true;
boolean interestedInBasektball = true;
boolean givesExtraEffort = true;
boolean femaleStudent = true;
boolean shootsFromDistance = false;
boolean fastRunningTimes = true;
boolean goodRebounder = true;
boolean goodBasketballDribbler = true;
// either qualification will work for the needed value
capableOfPlayingBasketball = tall || athleticallyInclined;
// requires both capable and female
qualifiedForPlayingWomensBasketball = capableOfPlayingBasketball
&& femaleStudent;
// requires both qualified and extra effort
QualifiedToMakeTheWomensBasketballTeam = qualifiedForWomensBasketball
&& givesExtraEffort;
Now, notice that this statement could have been made as follows but when it starts getting complicated like this, you are better off to abstract one or more of the logical components as was done previously (i.e., capableOfPlayingBasketball represented tall || athleticallyInclined, and qualifiedForPlayingWomensBasketball represented capableOfPlayingBasketball && femaleStudent.
// covers all the conditions,
// but may get too complicated in one statement
QualifiedToMakeTheWomensBasketballTeam = ( tall || athleticallyInclined )
&& femaleStudent && givesExtraEffort;
Here are a couple more examples using this system.
// combination of conditions required
capableOfPlayingGuard = ( shootsFromDistance ||
fastRunningTimes ) && goodBasketballDribbler;
// both necessary for successful center
capableOfPlayingCenter = tall && goodRebounder;
Here is another example.
boolean short = true;
boolean maleStudent = true;
boolean athleticallyInclined = true;
boolean interestedInBaseball = true;
boolean strongThrowingArm = false;
boolean goodCatchingSkills = true;
boolean goodBattingAverage = true;
boolean fastRunningTimes = true;
boolean givesExtraEffort = true;
// usually one or the other will get someone started in baseball
// note the use of the NOT operator on the individual boolean variable
capableOfPlayingBaseball = !short || athleticallyInclined;
// requirement for being a pitcher
capableOfPitching = capableOfPlayingBaseball && strongThrowingArm;
// requirements for playing a base
capableOfPlayingABase = capableOfPlayingBaseball
&& ( GoodCatchingSkills || GoodThrowingArm );
// requirements for being a pinch hitter
capableOfPlayingPinchHitter = goodBattingAverage && fastRunningTimes;
// requirements for making the team
qualifiedToMakeTheMensBaseballTeam
= ( capableOfPitching || capableOfPlayingABase
|| capableOfPlayingOutfield
|| capableOfPlayingPinchHitter )
&& givesExtraEffort && maleStudent;
qualifiedToBeTheWaterCarrier = !qualifiedToMakeTheMensBaseballTeam;
These athletic examples might be a little on the silly side but they represent a large number of opportunities for you to look at how Boolean expressions are put together and how they are used. The next section provides an opportunity for you to get closer to developing programming code with these tools.