|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
The boolean functions are && (“and”) and || (“or”). For instance,
((30 <= choice) && (choice < 33)) |
((newFace < 1) || (newFace > 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 | expr2 | (expr1||expr2) |
false | false | false |
false | true | true |
true | false | true |
true | true | true |
Exercise: What is the value of:
( ((3 <= 7) || (4 == (2+3))) && (Math.sqrt(4) > 0.0) ) |
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 |
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) __ ________ __ _______ _; } |
/** * @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 ); } |
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.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. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2008, Ian Barland, Radford University Last modified 2008.Feb.06 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |