jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes if loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "if loop" Watch "if loop" New topic

if loop

ashok kumar
Ranch Hand

Joined: Feb 28, 2005
Posts: 30
what is the difference between

int i=100;//error


{int i=100;}//no error

can anybody tell the difference.
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
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.

Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
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.

SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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

Joined: Mar 01, 2005
Posts: 485
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

Joined: Mar 02, 2005
Posts: 580
int i=100;//error


{int i=100;}//no error

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

Because according to JLS a variable should always declare within a block
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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:

Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Because it does not fall under the definition of a reachable statement.

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.

Tony Morris
Java Q&A (FAQ, Trivia)
udhaya kanagaraj
Ranch Hand

Joined: Mar 31, 2005
Posts: 62
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.
I agree. Here's the link: http://aspose.com/file-tools
subject: if loop
It's not a secret anymore!