Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Blank if statement

 
Brian Pianczk
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very nice explanation, thanks much.

Brian
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do you NEED it?

you don't.

but sometimes the logic can be cleaner if you do.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Empty blocks should never occur. Ever. The least you should put into a block is a comment on why the block is empty.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic