Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Jerry Stuckle Newsgroups: comp.lang.java.programmer Subject: Re: Simple if-exercises Date: Thu, 02 Apr 2015 15:08:29 -0400 Organization: A noiseless patient Spider Lines: 53 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit Injection-Date: Thu, 2 Apr 2015 19:07:39 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="e5dfcaf843262377062d568c34777fa3"; logging-data="13574"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18EyDm1J7AhLhfaRpE/1Xh0dbtdOk9b3Io=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 In-Reply-To: Cancel-Lock: sha1:eJsU3bmr92VYUX9+1bDMJbc9krM= Xref: csiph.com comp.lang.java.programmer:31442 On 4/2/2015 10:53 AM, Jeff Higgins wrote: > On 04/02/2015 09:58 AM, Jeff Higgins wrote: >> On 04/01/2015 08:10 PM, Stefan Ram wrote: >>> I was looking for very simple if-exercises. >>> >>> I usually found exercises like this: >>> >>> »Print "positive" if x > 0, "non-positive", otherwise.« >>> >>> The expected answer would be >>> >>> if( x > 0 ) >>> System.out.println( "positive" ); >>> else >>> System.out.println( "non-positive" ); >>> >>> However, I already have taught »?:«, so the better >>> (shorter) solution would be: >>> >>> System.out.println( ( x > 0 ? "" : "non-" )+ "positive" ); >>> >>> . Do you know a very simple if-exercise whose solution >>> cannot be written more tersely with the ternary operator >>> or where the formulation with »if« is clearly better than >>> with the ternary operator? >>> >>> Knowledge that can be assumed: calls and declarations of >>> static methods, +, -, *, /, usage of variables, declarations >>> of local variables, boolean, int, double, String, literals, >>> the block {...}, ?:, <, >, <=, >=, !=, ==. >>> >> Why don't we have a Quinary operator? >> x ? y LT : EQ : GT >> > Or Senary might be better - avoids an unchecked exception. > x ? y LT : EQ : GT : NC > Why not? FORTRAN has arithmetic IF statements, where the branch is made dependent on whether the result of the expression is less than, equal to or greater than 0. I found it quite useful at times when I was doing FORTRAN II programming. Of course, it also has a computed GOTO statement, but that's similar to (but more restrictive than) switch/case statements. Of course, it's also not structured programming. -- ================== Remove the "x" from my email address Jerry Stuckle jstucklex@attglobal.net ==================