Java seems to have a problem with if conditions that take up more than one
line.
What do you do if you have an if condition that does take more than one
line?
Thanks.
Janwillem Borleffs - 06 Nov 2005 16:59 GMT
> Java seems to have a problem with if conditions that take up more
> than one line.
Depends on where you break it. Something like the following should always
work:
if ((condition) ||
(condition) ||
(condition)) {
...
}
> What do you do if you have an if condition that does take more than
> one line?
When an if has more than two conditions, it's time to see if it's possible
to refactor the code. The more conditions an if-statement contains, the
messier and less maintainable it will be.
JW
VisionSet - 06 Nov 2005 17:25 GMT
> Java seems to have a problem with if conditions that take up more than one
> line.
>
> What do you do if you have an if condition that does take more than one
> line?
Do you mean the condition or the scope of the if ?
Sounds like the latter since there is never a problem with multi line
conditions because Java is free format ie the compiler ignores all white
space between constructs. Obviously you can't put white space within
constructs.
You must use braces for scope if you have more than 1 statement to execute
within the if block
ie
if (myBoolean) meth1(); // is fine
but
if (myBoolean)
meth1();
meth2();
will only execute meth1(), if you want both you need:
if (myBoolean) {
meth1();
meth2();
}
Some people do say always use braces, but I think it is fine for cases where
you really will only execute 1 method, but then I always put it on one line
to distiguish this intent:
if (myBoolean) meth1();
--
Mike W
Alan Krueger - 06 Nov 2005 17:26 GMT
> Java seems to have a problem with if conditions that take up more than one
> line.
Java, C/C++, C#, and similar languages are not line-based. You
you might want to make sure you're not seeing the syntax error
because it's spread over multiple lines.
(As an example of what I mean, there's an extra word in the above
text that you might have missed because the text was spread over
multiple lines.)
Roedy Green - 06 Nov 2005 23:22 GMT
>Java seems to have a problem with if conditions that take up more than one
>line.
>
>What do you do if you have an if condition that does take more than one
>line?
There are no known problems. If you think you have found one, please
post it.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.