http://en.wikipedia.org/wiki/%3F:
if (age < 4) {
System.out.println("Kid");
} else if (age > 18) {
System.out.println("Adult");
} else {
System.out.println("Not yet supported");
}
~
its a simple conditional statement that can also be written as follows
if(age < 4)
System.out.println("kid");
else if(age>18)
System.out.println("Adult");
else
System.out.println("Not Yet Supported");
This format is in every basic programming language like C, C++ and Java and is called the conditional if statement.
> > System.out.println((age < 4)?"Kid":(age >
> 18)?"Adult":"Not yet supported");
>
Agree w/ the statements above. I have to say though that this line isn't quickly readable and in my opinion would make debugging somewhat more difficult. It's starting to smell like C here.
> > > > System.out.println((age < 4)?"Kid":(age >
> > 18)?"Adult":"Not yet supported");
> >
>
> Agree w/ the statements above. I have to say though
> that this line isn't quickly readable and in my
> opinion would make debugging somewhat more difficult.
> It's starting to smell like C here.
Disagree with that. Once you know about the ternary operator, it's much easier to read than the expanded conditional logic, IMHO. Of course, this relies on one knowing about the ternary operator, but then, a working knowledge of the language should be a fair assumption to make in any case.
The java terniary short circuits.. for example
applet.size = (args.length == 0 ? 5 : Integer.parseInt(args[0]));
From memory, this would fail in C, because both possibilities are evaluated and then the correct one is chosen.
... and here's one I used yesterday ...
for (int i=0; i<TIMES; i++) {
double x = random.nextDouble(low, high);
System.err.format("%-11.2f%s", x, (i+1%4==0?"\n":"\t") );
if(x><low || x>high) throw new AssertionError("x="+x+" is not between "+low+" and "+high);
if(x==low||x==high) {
System.err.println("Bingo x="+x+"!"); break;
}
}
The equivalent "long hand" would make the debug code bigger than the real code.
> >
> > Disagree with that. Once you know about the
> ternary
> > operator, it's much easier to read than the
> expanded
> > conditional logic,
>
> I disagree.
You're wrong ;-)
Seriously, though, it's often better to be verbose and aim for clarity, sure, but sometimes a sprinkling of conditional logic is so trivial I'd prefer to confine it to a single line using the ternary operator. Mentally, when reading code, I'll find myself narrating a set of if statements, along the lines of "if that evaluates to true, do that. If it evaluates to false, do that." or similar, whereas ternary logic seems to just pop in and out of my train of thought in an instant: "it's either that or that" and we're done.
I have a natural aversion to "if" statements anyway. Been bitten by some nasty ones :-)
> > I can't possibly smell of elderberries. Any fruity
> > smell at the moment is massively overwhelmed by
> the
> > unmistakable stench of someone who's just got back
> > from a heavy metal festival
>
> ahh, the sweet smell of wee :-)
Yeh, you can probably detect that under the whiff of highly-dubious sweat