• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

if statement equal question

 
Ranch Hand
Posts: 62
Notepad Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Experts !

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

Thank you so very much !

kind regards,
marius

 
Bartender
Posts: 563
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marius Constantin
Ranch Hand
Posts: 62
Notepad Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic