This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if loop

 
ashok kumar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between

if(true)
int i=100;//error

And

if(true)
{int i=100;}//no error

can anybody tell the difference.
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
int i =0; is not a executable statement. It is declaration statement. if you omit the curly braces in if condition it execute the next line of the execute statement only. But here declaration statement is present. So compiler shows an error message.

if condition block has curly braces, then declaration statements and executables statements are allowed. These variables are available only in this scope. If executable statement is not present it doesn't show an error message.
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem is that int i = 100; is not a statement.
But if you use {} the compiler does not check if the block begins with statement, because it is not necessary.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not able to find such problem, i dont think so there is any problem with the code, its fine
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi animesh
why it is not throwing an unreachable statement when we put false inside the if condition.

can u tell me whats the condition to give compile time error saying the unreachable statement
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(true)
int i=100;//error

----------------------------->Error

if(true)
{int i=100;}//no error

------------------------------> No Error

Because according to JLS a variable should always declare within a block
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Parameshwaran,
About unreachable, it has been discusses before. Please go thru it
Its quite confusing, try to understand, if not then leave it,
its not going to be in SCJP exam, u dont have to worry abt these:

unreachable
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it does not fall under the definition of a reachable statement.
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#236365

The bytecode from the two source samples provided by the original poster will be exactly the same. One is considered poor form; always surround statement blocks with braces, even if a statement block has only one statement. To not follow this simple, yet fundamental rule, is reasonable grounds for death by firing squad.
 
udhaya kanagaraj
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai ashok,
I think JLS specified that every declaration statement should be placed inside a curley braces ,otherwise it will show two errors.they are
1 .class expected.
2 not a statement.
regards
udhayakp
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic