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

Blank if statement

Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
Hello all I tried searching blank if statement and couldn't find an answer to my specific problem.
I want the program to do nothing (but continue on) if the if is true.

Is it possible to have a blank if and just use the else?

Or is there a better way to word my if, so I can swap the two? I cant think of a way to use != because if it is !=0 then it will stop and 0 is a legal entry.



fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

The easiest way to see if you can have a blank 'if' block it to try it for yourself. hint - most people will tell you to always use curly braces, even though you don't need them around your single statement.

is there a reason you can't do

if (!(a[i] == '0' || a[i] == '1'))

sure it's ugly, but it might work.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Blank if body is valid but it is useless. If you have nothing to do in some condition then what is the need to check that condition.


Now in your case,



Are you looking for something else?
Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
fred rosenberger wrote:The easiest way to see if you can have a blank 'if' block it to try it for yourself. hint - most people will tell you to always use curly braces, even though you don't need them around your single statement.

is there a reason you can't do

if (!(a[i] == '0' || a[i] == '1'))

sure it's ugly, but it might work.


Thanks much that worked, I had no idea i could through a ! before the whole statement.

I am not sure what you mean about the curly braces? Does that refer to {} after any for loop?

thanks again
Brian
Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
Vishal Pandya wrote:
Now in your case,



Are you looking for something else?


I think the problem with that is the first a could be 1 and would execute the "Bad Input"? Or am I reading that wrong?

I do like the enhanced for loop, but I am not sure when to use it or not.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

here is your code:



a better way to write it would be



Note that i have curly braces around the one line of the if-block. It's ALWAYS a good idea to have your curly braces even if you only have one line. It will save you headaches later when you want to alter/debug your code and you try sticking in a line here or there, and end up breaking things... just trust me.

The problem with your original code is (i assume) you were trying to do this:



with NOTHING between the 'if'-statement and the else. you can simply do this:



even though the curly braces are empty, they are necessary.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

according to DeMorgan's law:

!(a[i] == '0' || a[i] == '1') becomes

!(a[i] == '0') && !(a[i] == '1')

which is equivalent to

a[i] != '0' && a[i] != '1'

(i think this is right)

I know that "if(a != '0' || a!= '1')" cant be right, because it will always be true.
Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
very nice explanation, thanks much.

Brian
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Brian Pianczk wrote:I think the problem with that is the first a could be 1 and would execute the "Bad Input"? Or am I reading that wrong?
Caught me . Sorry!
It should be
Brian Pianczk wrote:I do like the enhanced for loop, but I am not sure when to use it or not.


You can try this or this one.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
fred rosenberger wrote:...even though the curly braces are empty, they are necessary.

Fred, I wonder what is good with empty if body?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

Vishal Pandya wrote:
fred rosenberger wrote:...even though the curly braces are empty, they are necessary.

Fred, I wonder what is good with empty if body?

if you want an empty if-block, you need the braces.

Now, some would say that if your code requires an empty if-block, you need to re-write your code. putting the '!' in front of the entire if condition can do it, but that sometimes leads to ugly code. for example

if (!(isNotEmpty))

tell me in less than 2 seconds what this means...

it may be better to re-factor that variable name... but that could lead to confusion elsewhere...

ultimately, it comes down to what makes the cleanest, easiest to read code.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
fred rosenberger wrote:
Vishal Pandya wrote:
fred rosenberger wrote:...even though the curly braces are empty, they are necessary.

Fred, I wonder what is good with empty if body?

if you want an empty if-block...
But why do you need it at first place?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

why do you NEED it?

you don't.

but sometimes the logic can be cleaner if you do.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Empty blocks should never occur. Ever. The least you should put into a block is a comment on why the block is empty.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Blank if statement