File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If Statement Problem

 
christopher persinger
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Point taken, Jesper Young.
 
James A Anderson
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
James A Anderson
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So do you think that ResultSet is a good API?
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic