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


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

Re: Simple if-exercises

Started byEric Sosman <esosman@comcast-dot-net.invalid>
First post2015-04-02 08:58 -0400
Last post2015-04-02 08:58 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#31439 — Re: Simple if-exercises

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-04-02 08:58 -0400
SubjectRe: Simple if-exercises
Message-ID<mfjebm$i5s$1@dont-email.me>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web