if(i < j < k) ...

Hi, I am wondering why Java does not support constructs like this:


int i, j, k; // initialized with any values
if(i < j < k) {
  // do something
}

Would it be so difficult (ambiguous) to evalute the expression i < j < k? Let’s say with a left-to-right rule the compiler could first rewrite it as i < j && j < k and then continue compilation.

Johannes

PS: Maybe it is supported in Mustang and I simply don’t know it yet.

I spose cause what you could read it as is:

(i > j) > k

Which ends up being boolean > int - which makes no sense at all. (unless of course you’re in C)

Kev

its not very nice for readability and would be more mistake prone.

on the subject of additions to the language i wouldn’t mind a quicker way to do a for loop, something like

for (i : 10);

which basically loops 10 times sort of a short cut to

for(int i = 0; i < 10; i++);

but then again could just be considered as complicating the language.

yeah that would complicate it because how can:


for (i:10);

possibly know that:

  1. i is an int
  2. it needs to stop looping when i < 10; as compared to i<=10 or i > 10 or whatever
  3. it needs to increment i by only one

ok maybe that was a bad idea ;D

but still the normal for loop is a little long winded if you just wanna loop a piece of code a given amout of times
how about

for (10) {}

to just loop 10 times

ok I have to give you that one :wink:

of couse, half the time you run code a set number of times, you’re looping through an array. but with:


for (10) {}

how can you know what array index your’e at without using a variable? :slight_smile:

p.s. I’m just messing with you, not shooting you down :stuck_out_tongue:

There are tons of places where the language could “benefit” from small and fancy constructs. But java focuses on not allowing too much fancy stuff to increase readability, which can be particularly important when programmers are maintaining code written by other programmers. Having a small syntax is a beautiful thing in itself. Still, sometimes I suspect we would all like a bit of operator overloading or likewise.

setting that aside, would you ever use it for more then 3 arguments? (example (a>b>c>d) ) I mean in anything remotely trivial. (sure there can be cases)
is it perhaps that your looking for something like if(number.between(a,b)); or (worse) you could sacrifise a nother word to the reserved keywords.

deplexing syntax :-\ it’s dangerous.
I can relate, thinking back at math classes, that you’d want something like that. in java your assigning, not defining however.

Hi,

thanks for all the replies! I see the point that Java does not try to be a “mathematical” language and can not make syntactical exceptions for those special cases in order to keep the syntax small and consistent (although I find serveral odd/inconsistent notations like the ‘…’ argument for method parameters, or the array declaration where String[] args; is possible as well as String args[];).

yeah, true… but it would only require to expand the ‘<’ operator to three arguments (< p1 p2 p3) with the exceptional notation, that the second ‘<’ needs to be ignored :stuck_out_tongue: okay… not very nifty

Suppose not… at least not that I could recall. I encountered the situation where I needed three arguments quite often, but never more.

Johannes

also, that would be slower than i < j && j < k, since if i < j evaluates to false, the whole expression can’t be true anymore and it stops operating (try 0 && i++, it won’t increment). you lose that edge with your idea :stuck_out_tongue:

yeah, also true… but I could sacrifice the performance loss if there was a neat way to realize it. Speaking of ‘&&’, I just noticed that ‘&&’ is another additional enhancement for an ordinary operator, namely ‘&:slight_smile: Why not do something similar for ‘<’? But as a final word I agree that there are more important things to improve than those compare operators in question.

Johannes

huh? why? && and & are different operators used for compleyely different purposes…

not sooo different… both are AND operators. The first one is evaluated in a bitwise manner and the second one conditionally which allows to neglect the evaluation of the right side of the operator if the left side turns out to be false. That’s all (as far as I know). But at the bottom line, both a&b and a&&b only hold if a and b are true.

I know i know, you can use && for nice things like if(pointer != null && pointer.doSomethingAndReturnABoolean()) which would not work with & because both sides of the operator are going to be evaluated regardless of the outcome of the other. This only underlines my statement, that some handy constructs that are the enhancement of an existing one have been already introduced to Java ;D I guess it all depends on the way of seeing it…

Johannes

[quote]also, that would be slower than i < j && j < k, since if i < j evaluates to false, the whole expression can’t be true anymore and it stops operating (try 0 && i++, it won’t increment). you lose that edge with your idea
[/quote]
i < j < k could easily be optimized in &&-style, unless we choose that it should always evaluate all arguments. However we would also need i > k > j, otherwise we couldn’t specify the order in which the checks should be done (which can be important).

Most people don’t want Java bloated with everybody’s pet idea. You can always write a preprocessor If you feel an irresistable urge to mess with the syntax.