File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes if statement equal question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "if statement equal question" Watch "if statement equal question" New topic
Author

if statement equal question

Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
Posts: 57

Hello Experts !

I was wondering. is this piece of code similar with this one ?

Thank you so very much !

kind regards,
marius

Greg Brannon
Rancher

Joined: Oct 24, 2010
Posts: 380
Well, both of them are valid and do nothing if a < b, so in that way they're similar. What do you really want to know?


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
Posts: 57

Greg Brannon wrote:Well, both of them are valid and do nothing if a < b, so in that way they're similar. What do you really want to know?


Thank you very much for your help !

I just wanted to know if this two pieces of code are 2 different ways to express the same thing. In case a < d do nothing.
And the answer is yes, right ?

thank you so much,
marius
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 8428

here's another way to do it:

since you don't do anything different if a >= b, it's just like doing nothing at all

On a more serious note, if you have to do something like this, i would always go with the latter. it is EXTREMELY easy to overlook that semi-colon later on when you come back and decide you DO want to do something when a < b. If you put in the braces now, it's relatively trivial to drop in your new code.

If you don't, odds are high that at some point, you will come back, add the braces, overlook the semicolon, and pull your hair out for a while when your code doesn't work how you think it should.


Never ascribe to malice that which can be adequately explained by stupidity.
Rishiraj Bayerd
Greenhorn

Joined: Aug 06, 2007
Posts: 9
It is do-nothing situation with if block.
The first snippet just act as an instruction. That is, the execution of just immediate instruction of if
never depends it.

The second snippet is an if -block with empty scope. So, there are nothing to execute after result of the if-condition become true.

So, the result of the snippets become same.
 
IntelliJ Java IDE
 
subject: if statement equal question
 
Threads others viewed
First Ant project not compiling
Problem with casting
Returning array To Method
Inner Class
Java From The Command Line - Communicating With The User
developer file tools