views:

969

answers:

7

Why can't do you this if you try to find out whether an int is between to numbers:

if(10 < x < 20)

Instead of it, you'll have to do

if(10<x && x<20)

which seems like a bit of overhead.

+6  A: 

Because that syntax simply isn't defined? Besides, x < y evaluates as a bool, so what does bool < int mean? It isn't really an overhead; besides, you could write a utility method if you really want - isBetween(10,x,20) - I wouldn't myself, but hey...

Marc Gravell
Strangely enough I would expect the first argument of a `isBetween()` method to be the value to be tested and the second and third one to be the upper and lower bounds. Don't ask me to give you a reason, it simply feels more sane this way.
Joachim Sauer
I wasn't going to get excited worrying about the order, to be honest. Hey, I'm mainly a C# guy, so I could add an extension method: `if(x.IsBetween(10,20)) {...}` ;-p
Marc Gravell
@Marc: Why not take it further: if (x.IsBetween(10).And(20)) :-)
Cellfish
@Cellfish: fluid API is all good and shiny, but you're taking it too far: What does `x.IsBetween(10)`?
Joachim Sauer
@Joachim - some transient interface. Actually, this pattern (with a fluent API) is not uncommon in (for example) .NET testing frameworks.
Marc Gravell
what kind of answer is that? did the OP not know that the syntax isn't define? do you suggest that ternary operator cannot exist?
irreputable
re ternary - not in the least I suggest that this *particular* ternary syntax doesn't exist, and if it did it would probably need some tweaks to disambiguate from the existing `<` etc (as discussed).
Marc Gravell
A: 

Because the < operator (and most others) are binary operators (they take two arguments), and (true true) is not a valid boolean expression.

The Java language designers could have designed the language to allow syntax like the type you prefer, but (I'm guessing) they decided that it was not worth the more complex parsing rules.

Michael Petrotta
+8  A: 

It's just the syntax. '<' is a binary operation, and most languages don't make it transitive. They could have made it like the way you say, but then somebody would be asking why you can't do other operations in trinary as well. "if (12 < x != 5)"?

Syntax is always a trade-off between complexity, expressiveness and readability. Different language designers make different choices. For instance, SQL has "x BETWEEN y AND z", where x, y, and z can individually or all be columns, constants, or bound variables. And I'm happy to use it in SQL, and I'm equally happy not to worry about why it's not in Java.

Paul Tomblin
that is just silly. 12<x!=5 is by no means a reasonable feature request, while 10<x<20 definitely is. jesus.
irreputable
yet just such a construct can be used in python.
GregS
Actually, I like Marc's answer better than mine.
Paul Tomblin
+4  A: 

COBOL allows that (I am sure some other languages do as well). Java inherited most of it's syntax from C which doesn't allow it.

TofuBeer
It is obvious that a lot of water have passed under the bridges since I last developed something in COBOL because I cannot remember that. Just because I am nuts and want to refresh my memory, can you please comment with a line of caps :-)
Fredrik
I AM PRETTY SURE I REMEMBER DOING THAT IN COBOL (sniff)
TofuBeer
Python allows this as well.
Stephan202
obviously with different syntax that is :)
TofuBeer
@TofuBeer: Ok, I asked for it :-D
Fredrik
Common Lisp allows any positive arity for such comparison operators. In the case of `<', the arguments must be monotonically increasing for it to evaluate to `t'. See http://www.lispworks.com/documentation/HyperSpec/Body/f_eq_sle.htm#LT.
seh
+2  A: 

You are human, and therefore you understand what the term "10 < x < 20" suppose to mean. The computer doesn't have this intuition, so it reads it as: "(10 < x) < 20".

For example, if x = 15, it will calculate:

(10 < x) => TRUE

"TRUE < 20" => ???

In C programming, it will be worse, since there are no True\False values. If x = 5, the calculation will be:

10 < x => 0 (the value of False)

0 < 20 => non-0 number (True)

and therefore "10 < 5 < 20" will return True! :S

Oren
I know how it works. But aren't programming languages not supposed to make things easy for humans? Otherwise we would still be writing assembly or in binary codes.
Timo Willemsen
That's an important point: Making "10 < x < 20" illegal is actually more helpful than doing it the C way (which makes the expression valid, but it doesn't do what a non-C programmer would expect it to do).
Joachim Sauer
Timo - you might find this article of interest: http://blogs.msdn.com/ericgu/archive/2004/01/12/57985.aspx. Remember, someone's got to implement your request, and debug, test, and maintain it.
Michael Petrotta
But wouldn't it be even more helpfull if 10 < 5 < 20 would just evaluate false :p
Timo Willemsen
Someone thought that multiple inheritance and macros were a good idea too. Maybe, but at what cost?
Michael Petrotta
Oren
+3  A: 

The inconvenience of typing 10 < x && x < 20 is minimal compared to the increase in language complexity if one would allow 10 < x < 20, so the designers of the Java language decided against supporting it.

starblue
+2  A: 

One problem is that a ternary relational construct would introduce serious parser problems:

<expr> ::= <expr> <rel-op> <expr> |
           ... |
           <expr> <rel-op> <expr> <rel-op> <expr>

When you try to express a grammar with those productions using that using a typical PGS, you'll find that there is a shift-reduce conflict at the point of the first <rel-op>. The parse needs to lookahead an arbitrary number of symbols to see if there is a second <rel-op> before it can decide whether the binary or ternary form has been used. In this case, you could not simplely ignore the conflict because that would result in incorrect parses.

I'm not saying that this grammar is fatally ambiguous. But I think you'd need a backtracking parser to deal with it correctly. And that is a serious problem for a programming language where fast compilation is a major selling point.

Stephen C
Not really. See http://docs.python.org/reference/expressions.html#notin and http://docs.python.org/reference/grammar.html for how Python handles it.
Adam Rosenfield
@Adam - that's weird. The grammar allows "1 < i < 3 < 4" and so on. I guess it is all sorted out by the semantic analyser.
Stephen C