• Post Reply Bookmark Topic Watch Topic
  • New Topic

if-then-else problem  RSS feed

 
Maxwell Xu
Greenhorn
Posts: 18
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear fellow programmers,

Hi, so I saw this happening and I didn't know what was going on.
So:

has the same output as:

even though in the second class, there weren't curly braces with the if statement. Can anyone tell me why?

Thanks,
Maxwell

P.S. If I'm not too good with posting posts, sorry, this is my first time posting.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, the code that you provided does *not* compile, but assuming that you fix the compile issue (and in the same way that I am envisioning), then to answer your question...

From the Java Language Specification, the IfThenStatement has many options; one of those options take a single Statement... and... one of the options for Statement is a BlockStatement. Of course, the curly braces is what bookends the BlockStatement.*

So, having a single statement is the same as a block containing a single statement -- as a block statement is a statement.

Henry 

* The bold items are terminology from the Java Language Specification.
 
Felipe Kunzler
Ranch Hand
Posts: 35
3
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

First of all, your code would probably give you a compile error in this line:
Here you are actually attributing a value (9) to the variable int HelloWorld, so it would result in which is not a boolean.

If you change your if's to == instead of = you would actually be comparing.

And the reason why both would give you the same output is because the curly braces can be omitted if the body has only one statement.
For example, this:

is the same as this:

It is worth noticing that omitting the braces is considered a bad practice since it reduces code readability, so just don't use it.

Cheers!
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Beware of that sort of code; your if statement would probably compile in C and run and produce an incorrect result
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maxwell Xu wrote:...
even though in the second class, there weren't curly braces with the if statement. Can anyone tell me why?

I think others have done it for you.

However, just to add to what they've said: Since:
  { some-statement; }
is identical to
  some-statement;
you should always include the braces - for consistency alone if nothing else.

So, my advice (at least for the moment): Behave - and write your code - as though the second option doesn't exist.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!