aspose file tools*
The moose likes Beginning Java and the fly likes If Statement Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "If Statement Problem" Watch "If Statement Problem" New topic
Author

If Statement Problem

christopher persinger
Greenhorn

Joined: Aug 23, 2005
Posts: 20
Hello all. I am having trouble with using the && operator in an if-statement. The if-statement is only checking the first condition and will not even go past the && operator in the if-statement header. Here is the header for my if-statement, let me know if I am doing something wrong. Thanks in advance.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
That is normal behaviour for the && operator. If the term preceding the && in a boolean statement evaluates to false, the && operator assumes the whole expression will evaluate to false, and stops looking.

If you need to evaluate all parts of the expression (which appears unnecessary in the case you quoted) use the & operator.
See H M Deitel, P Deitel, Java How To Program (6/e) Deitel Inc/Prentice-Hall, Upper Saddle River NJ (2005) page 204.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

Campbell Ritchie: "If the term preceding the && in a boolean statement evaluates to false, the && operator assumes the whole expression will evaluate to false, and stops looking."

The && operator does not assume that the whole expression will evaluate to false - if one of the expressions to the right or left of && is false, then the whole expression is false. There's no "assumption" there, it's just logic. Remember:

false && false == false
false && true == false
true && false == false
true && true == true

Java doesn't need to look any further if the expression on the left of the && is false, because then the answer of the expression is certainly false.

Campbell Ritchie: "If you need to evaluate all parts of the expression (which appears unnecessary in the case you quoted) use the & operator."

No, don't do that! That's an ugly hack. Don't rely on people understanding this subtle difference between & and &&. If you have expressions in the statement that have side effects and therefore must be executed, just write your code differently, for example:

[ March 22, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Point taken, Jesper Young.
James A Anderson
Greenhorn

Joined: Mar 02, 2007
Posts: 12
Although the statment about the compiler making an assumption about the expression was poor and incorrect, I disagree with the statement "Don't rely on people understanding this subtle difference between & and &&."

I think every programmer should understand the difference between regular logical operators and short-circuit operators and use them appropriately. That is why they are in the language - for programmers to take advantage of them, not to be avoided because you don't understand "the subtle difference between them."

When inverviewing potential programming candidates, I always ask them about short-ciruit operators, because I think it is important to know how they work. Even if you do avoid using them yourself because you don't understand them, you may have to work on somebody elses code where they are used and it is important to understand whether an expression is going to be evaluated or not.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

Even if you do avoid using them yourself because you don't understand them, you may have to work on somebody elses code where they are used and it is important to understand whether an expression is going to be evaluated or not.


James, you are making an assumption that Jesper "avoid using them" because he doesn't "understand them". How do you know that he avoid thems because he clearly understands that it is a source of potential problems?

Personally, I agree with both sides. It does raise an eyebrow when I see it used in a boolean expression, but I don't have a problem with it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
James A Anderson
Greenhorn

Joined: Mar 02, 2007
Posts: 12
Henry,

No, I based my post on Jesper's post. I'm not assuming anybody doesn't know the difference between the operators, in fact I am recommending programmers SHOULD know the difference. Jesper is basing his coding on the assumption that some people don't understand the difference.

Should we not use "=" either, since some people don't know the difference between "=" and "=="?
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
Doing anything that has side effects in the condition for an if statement obviously means you learned to code in a termite hill.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Originally posted by Ricky Clarkson:
Doing anything that has side effects in the condition for an if statement obviously means you learned to code in a termite hill.
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
So do you think that ResultSet is a good API?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

Originally posted by Ricky Clarkson:
So do you think that ResultSet is a good API?


I don't think that Paul was implying that JDBC is a good or bad API. I think Paul was just pointing out that you are generalizing, as there are APIs that can't be avoided -- it's not correct to call anyone that uses JDBC, in the recommended way, as "learned to code in a termite hill".

Henry
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
There was a little sarcasm in my original termite hill post, based on this text from a previous post:

Don't rely on people understanding this subtle difference between & and &&. If you have expressions in the statement that have side effects and therefore must be executed, just write your code differently


If you're comfortable with using assignments inside if statement conditions, feel free. If someone else isn't comfortable with that, hopefully they have tools that will present a more appropriate view of the same source code.

Personally I set my IDE to flag those as a warning, but then I'm pretty much on a solo project.

JDBC is an awful API.
 
jQuery in Action, 2nd edition
 
subject: If Statement Problem