statement not working I expect

I have a loop going and would like to make the code as compact as possible ... this loop is below ...

for (int i=0 ; i<a.length ; ++i){

if (direction == Direction.asc){

if (a[i] > a[after] && a[i] < boundary)

boundary = a[returnval = i];

}elseif (direction == Direction.desc){

if (a[i] < a[after] && a[i] > boundary)

boundary = a[returnval = i];

}

}

but should this not work as well ?

for (int i=0 ; i<a.length ; ++i)

if (direction == Direction.asc)

if (a[i] > a[after] && a[i] < boundary)

boundary = a[returnval = i];

elseif (direction == Direction.desc)

if (a[i] < a[after] && a[i] > boundary)

boundary = a[returnval = i];

I dont see why not, but it doesnt work as expected.

Anyone care to shed some light on this?

[1523 byte] By [XyuRia] at [2007-10-2 18:09:05]
# 1
> I have a loop going and would like to make the code> as compact as possible Why?
XyuRia at 2007-7-13 19:28:45 > top of Java-index,Java Essentials,Java Programming...
# 2

> I have a loop going and would like to make the code as compact as possible ... this loop is below ...

Don't worry too much about making it compact in terms of number of lines or anything else. Make it readable and correct.

> for (int i=0 ; i<a.length ; ++i) {

> if (direction == Direction.asc) {

> if (a[i] > a[after] && a[i] < boundary)

> boundary = a[returnval = i];

> } else if (direction == Direction.desc) {

> if (a[i] < a[after] && a[i] > boundary)

> boundary = a[returnval = i];

> }

> }

> but should this not work as well ?

> for (int i=0 ; i<a.length ; ++i)

> if (direction == Direction.asc)

> if (a[i] > a[after] && a[i] < boundary)

> boundary = a[returnval = i];

> else if (direction == Direction.desc)

> if (a[i] < a[after] && a[i] > boundary)

> boundary = a[returnval = i];

> I dont see why not, but it doesnt work as expected.

>

> Anyone care to shed some light on this?

No, they shouldn't work the same. The "else" gets connected to the wrong "if". That's one reason why it's always good to write the { and }, even if there is only one line in the 'if', 'else', 'for', 'while', etc.

And the following is just nasty:

boundary = a[returnval = i];

I know what it does, but I bet a lot of people would get confused if they saw it. It looks error-prone, too.

MLRona at 2007-7-13 19:28:45 > top of Java-index,Java Essentials,Java Programming...
# 3
The two for-loops aren't the same.Try using {} with every if and else. It just gets confusing otherwise.
pbrockway2a at 2007-7-13 19:28:45 > top of Java-index,Java Essentials,Java Programming...
# 4

> > I have a loop going and would like to make the

> code

> > as compact as possible

>

> Why?

Its just how I am ! afaik the java language allows code to be written the way I want it and have done so for a while now ... but this time it didnt for some reason and I would like to know why :p

XyuRia at 2007-7-13 19:28:45 > top of Java-index,Java Essentials,Java Programming...
# 5

> I have a loop going and would like to make the code

> as compact as possible ...

You're not making it "compact", you're making it error-prone. This example should teach you best.

With proper formatting, this is what your second loop looks like:

for (int i=0 ; i<a.length ; ++i)

if (direction == Direction.asc)

if (a[i] > a[after] && a[i] < boundary)

boundary = a[returnval = i];

else if (direction == Direction.desc)

if (a[i] < a[after] && a[i] > boundary)

boundary = a[returnval = i];

da.futta at 2007-7-13 19:28:45 > top of Java-index,Java Essentials,Java Programming...
# 6
> And the following is just nasty:> boundary = a[returnval = i];hehe yeah ... it is :-) was aiming for a one liner though, which is the only reason its like that.
XyuRia at 2007-7-13 19:28:46 > top of Java-index,Java Essentials,Java Programming...
# 7

> With proper formatting, this is what your second loop

> looks like:

> > for (int i=0 ; i<a.length ; ++i)

> if (direction == Direction.asc)

> if (a[i] > a[after] && a[i] < boundary)

> boundary = a[returnval = i];

> else if (direction ==

> if (direction == Direction.desc)

> if (a[i] < a[after] && a[i] >

> < a[after] && a[i] > boundary)

> boundary = a[returnval =

>boundary = a[returnval = i];

>

Aaaaaah, i got it now :-) yeah, the way I had it indented made reading the straucture quite misleading ! Thank you all for your help :-)

XyuRia at 2007-7-13 19:28:46 > top of Java-index,Java Essentials,Java Programming...