RU beehive logo ITEC dept promo banner
ITEC 120
2008spring
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect04a
boolean connectives: and, or, not

boolean connectives: &&, ||, !

The boolean functions are && (“and”) and || (“or”). For instance,

((30 <= choice) && (choice < 33))
reads as “choice is at least thirty, and less than 33”. Similarly,
((newFace < 1) || (newFace > NUM_SIDES))
tests for newFace being less than 1, or bigger than NUM_SIDES.

In general, && looks at the boolean expressions on each side, and the entire && will evaluate to true if (and only if) the left side and the right side evaluate to true:

expr1 expr2 (expr1&&expr2)
false false false
false true false
true false false
true true true
(expr1 and expr2 can be any other boolean expression—including more-deeply-nested and/or expressions!)
Complementing &&, an entire || expression evaluates to true if (and only if) one side or the other evaluates to true:
expr1 expr2 (expr1||expr2)
false false false
false true true
true false true
true true true
Really, you can think of && as a function with the signature boolean &&( boolean b1, boolean b2 ) (although, like arithmetic1, we write them in infix notation, with the function-name coming inbetween its two inputs, rather than in-front-of-them-with-parentheses).

Exercise: What is the value of:

( ((3 <= 7) || (4 == (2+3))) && (Math.sqrt(4) > 0.0) )
This is just like solving an arithmetic problem, except that we are dealing with true and false instead of all the integers2.

It's worth a mention that in English, “or” is used in two different ways: The inclusive-or, which is what || means, means “one or the other or both”. For example, if the question on the tax form asks “are you over 18, or had income of more than $20,000”, and you meet both criteria, then the answer is true. (People might use “and/or” to emphasize they include the “and” part.) However, sometimes English uses “or” to mean one or the other but not both. For instance, “You can eat veal, or you can be morally responsible” is implying that one option precludes the other. (You may or may not agree with that presumption, of course.) Similarly, if you are told “Keep dating your ex, or date me!”, don't presume that the option of both is included in the “or”!

While we're on the topic, there is one last boolean operator, ! (“not”):

expr !expr
false true
true false
So (2+2 == 4) is true, but !(2+2 == 4) is false. This could also have been written directly using the not-equals operator: (2+2 != 4).

Exercise: What is

Let's revisit mustRegisterForDraft. The current laws are a bit different from what is shown: only men aged 18-25 must register. Update our function accordingly:

/**
 * @param age The age (in years) of the person in question.  E.g., 18months is age 1.
 * @param ___  _______________________________________________
 * @return whether or not the person must register with Selective Service.
 *   mustRegisterForDraft_v2( 0, ___) == false
 *   mustRegisterForDraft_v2( 0, ___) == false
 *   mustRegisterForDraft_v2(17, ___) == false
 *   mustRegisterForDraft_v2(18, ___) == true
 *   mustRegisterForDraft_v2(22, ___) == true
 *   mustRegisterForDraft_v2(22, ___) == false
 *   mustRegisterForDraft_v2(25, ___) == _______
 *   mustRegisterForDraft_v2(27, ___) == _______
 */
boolean mustRegisterForDraft_v2( int age, ____ ______ ) {
  return _ (age >= 18) __ ________ __ _______ _;
  }

solution

/**
 * @param age The age (in years) of the person in question.  E.g., 18months is age 1.
 * @param isFemale  Is the person in question female?
 * @return whether or not the person must register with Selective Service.
 *   mustRegisterForDraft_v2( 0, true ) == false
 *   mustRegisterForDraft_v2( 0, false) == false
 *   mustRegisterForDraft_v2(17, false) == false
 *   mustRegisterForDraft_v2(18, false) == true
 *   mustRegisterForDraft_v2(22, false) == true
 *   mustRegisterForDraft_v2(22, true ) == false
 *   mustRegisterForDraft_v2(25, false) == true
 *   mustRegisterForDraft_v2(27, false) == false
 */
boolean mustRegisterForDraft_v2( int age, boolean isFemale ) {
  return ( (age >= 18) && (age <= 25) && !isFemale );
  }
By the way, now we see more clearly, the advantage of making this its own function -- we don't have to update the rules in several places. In fact, we'll need to update our function further, to account for whether the person is a U.S. citizen, or an alien living in the U.S. In fact, ultimately your code should be a direct translation of the actual regulations, which can be fairly detailed.

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


Naming convention: start boolean variable names with a word to make it clear that the variable stores a true/false value: “isHappy”, “hasAllPrereqs”, etc.

Aside: Some languages — not Java — allow “?” to be part of a name; in those languages boolean variables are often named “happy?”, “hasAllPrereqs?”. Since functions which return a boolean value are sometimes called “predicates”, some people use the letter “p” to indicate a predicate function: “isPrimeP(int n)”, “flightIsBookedP( int flightNum )”.


1Just like we think of + and * as functions which take in two numbers, and return a number.      

2If you look at the tables and squint, thinking of false,true being like 0,1 and ||,&& being like +,* then these tables above really do look fairly similar to arithmetic.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2008, Ian Barland, Radford University
Last modified 2008.Feb.06 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme