Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: in1To10 Date: Sat, 09 Aug 2014 16:13:29 -0400 Organization: A noiseless patient Spider Lines: 84 Message-ID: References: <0c8de66a-429a-4697-b575-35f37a7b6882@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 9 Aug 2014 20:13:35 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="bd4e1cba8d8592bec5d5ed9e4537fbfd"; logging-data="11523"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+zvO04z7OVXj+uisHGMoPs" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.0 In-Reply-To: <0c8de66a-429a-4697-b575-35f37a7b6882@googlegroups.com> Cancel-Lock: sha1:+3KeGgDxPVDcQamCQmiNIAOpbHg= Xref: csiph.com comp.lang.java.programmer:29599 On 8/9/2014 3:12 PM, cdalten@gmail.com wrote: > from the url > > http://codingbat.com/prob/p137365 > > The problem states: "Given a number n, return true if n is in the range 1..10, inclusive. Unless "outsideMode" is true, in which case return true if the number is less or equal to 1, or greater or equal to 10." > > Here is what I came up with... > > public class In1to10 { > static boolean in1To10(int n, boolean outsideMode) { > > if (outsideMode == false){ > if (n >= 1 && n <=10) { > return true; > } > } else if (outsideMode == true){ > if (n <=1 || n >=10) { > return true; > } > } > > return false; > } > public static void main(String[] args) { > System.out.println(in1To10(5, false)); //true > System.out.println(in1To10(11, false)); //false > System.out.println(in1To10(11, true)); //true > }//end main > } > > > Now upon googling the solutions, I saw some people use > if(outsideMode == false) > if(outsideMode == true) > > instead of using if(outsideMode == false)/else(outsideMode == true). > > Would both catch all the cases? If not, which one would? Since a `boolean' can represent only the two values `true' and `false', it follows that - If it isn't `true' it must be `false', and - If it isn't `false' it must be `true'. Therefore, you can write if (outsideMode == true) { // Handle the `true' case } else { // Look, Ma: No test! // Handle the `false' case } ... using only one `if' test. If the test succeeds you know `outsideMode' is `true'. If the test fails you know `outsideMode' is *not* `true', hence (because of the limited possibilities for a `boolean') it must be `false'. Alternatively, you could write if (outsideMode == false) { // Handle the `false' case } else { // Look, Ma: No test! // Handle the `true' case } ... by the same reasoning: If it's not `false', it must be `true'. A few additional hints: - It's seldom if ever worth while to compare a boolean value to a boolean constant. Instead of `if (outsideMode == true)' just write `if (outsideMode)'. Or, if you want to be perverse, write `if (outsideMode == true == true == true)' ... - Writing `if (something) return true; else return false;' is kind of silly, since `return something;' does the same thing. Similarly, `if (something) return false; else return true;' can be written as `return !something;'. -- esosman@comcast-dot-net.invalid