Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31439

Re: Simple if-exercises

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Simple if-exercises
Date 2015-04-02 08:58 -0400
Organization A noiseless patient Spider
Message-ID <mfjebm$i5s$1@dont-email.me> (permalink)
References <if-20150402020010@ram.dialup.fu-berlin.de>

Show all headers | View raw


On 4/1/2015 8: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" );

     // "Better" is in the eye of the beholder:
     System.out.println(x > 0 ? "positive" : "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?

     Anything requiring more than one "action" on at least one
branch of the test would suffice.  Or, anything involving more
than one test.  Perhaps

	Given x,y from "Scissors", "Paper", "Stone", print "Scissors
	cut Paper" or "Paper covers Stone" or "Stone breaks Scissors",
	as appropriate.

would suit.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next | Find similar | Unroll thread


Thread

Re: Simple if-exercises Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-04-02 08:58 -0400

csiph-web