order in if statement

L

Leihcim

Hi,

If you have multiple tests in an if statement, is the order in which they
are evaluated guaranteed from left to right ? I.e. I want to do this:

ResultSet rs = // get result set.

if(rs!=null && rs.next()) {
// do something
}

The right test could throw an exception if it is evaluated first, hence
the left test. It works on my system, but is it guaranteed ?

Thanks, Michiel.
 
T

Thomas Kellerer

Leihcim wrote on 22.02.2009 13:52:
Hi,

If you have multiple tests in an if statement, is the order in which they
are evaluated guaranteed from left to right ? I.e. I want to do this:

ResultSet rs = // get result set.

if(rs!=null && rs.next()) {
// do something
}

The right test could throw an exception if it is evaluated first, hence
the left test. It works on my system, but is it guaranteed ?

Read the specs:

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#4779
 
D

Dmitriy Melnik

Leihcim said:
Hi,

If you have multiple tests in an if statement, is the order in which they
are evaluated guaranteed from left to right ? I.e. I want to do this:

ResultSet rs = // get result set.

if(rs!=null && rs.next()) {
        // do something

}

The right test could throw an exception if it is evaluated first, hence
the left test. It works on my system, but is it guaranteed ?

Yes. If the first parts equals to false Java will always stop the
evaluation because it becomes clear that all the statement will be
equal to false. Similarly, when you use || if the first part is true
the evaluation stops either.
 
E

Eric Sosman

Dmitriy said:
Yes. If the first parts equals to false Java will always stop the
evaluation because it becomes clear that all the statement will be
equal to false. Similarly, when you use || if the first part is true
the evaluation stops either.

One additional point: This is not a special property
of the `if' statement, but a property of the `&&' operator.
If the first operand is false, the second operand is not
even evaluated. Similarly for `||': If the first operand
is true, the second is not evaluated at all. Also, in
`x ? y : z' it is guaranteed that `x' is evaluated first,
and then exactly one of `y' or `z' is evaluated while the
other is not.

... and that's in any context, not just in `if'. It
holds for `while', `do...while', `for', and even for
simple assignment:

boolean doSomething = rs!=null && rs.next();

String name = (thing == null) ? "?" : thing.toString();
 
J

Joshua Cranmer

Leihcim said:
If you have multiple tests in an if statement, is the order in which they
are evaluated guaranteed from left to right ? I.e. I want to do this:

Expressions have a much stricter definition in terms of when they are
evaluated in Java than in C/C++. Specifically, it is left-to-right where
the operands are evaluated before the operators. Also, side effects
happen immediately after evaluation, most notably post-increment happens
after evaluation so the statement |i = i++;| does not affect the value of i.

&& and || (?: as well) are special--these are what is known as
"short-circuit operators." The right-hand side of && is evaluated only
if the left-hand side is true, while the right-hand side of || is
evaluated only if the left-hand side is false. ?: chooses which of the
two operands to evaluate based on whether or not the first operand is true.

The special effects of the latter are held constant from C, C++, or
other C-based languages. Well, unless you start overloading the && or ||
operators, but then you've opened your own can of worms.
 
R

Roedy Green

If you have multiple tests in an if statement, is the order in which they
are evaluated guaranteed from left to right ? I.e. I want to do this:

ResultSet rs = // get result set.

if(rs!=null && rs.next()) {
// do something
}

The right test could throw an exception if it is evaluated first, hence
the left test. It works on my system, but is it guaranteed ?

see http://mindprod.com/jgloss/mccarthyandoperator.html

--
Roedy Green Canadian Mind Products
http://mindprod.com

One path leads to despair and utter hopelessness. The other,
to total extinction. Let us pray we have the wisdom to choose correctly.
~ Woody Allen .
 
L

Lew

Leihcim wrote on 22.02.2009 13:52:
Thomas said:

Gods! At least show the current version! That one is over four years out of
date.

<http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html>
Ss. 15.23 and 15.24

Admittedly the semantics of && and || did not change in 2004, but still. Your
link could cause folks to bookmark it for general use, and then other things
they look up would be wrong. Give them the correct specs.
 
L

Lew

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top