aspose file tools*
The moose likes Java in General and the fly likes if-else statement problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "if-else statement problem" Watch "if-else statement problem" New topic
Author

if-else statement problem

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583


I can't expect so much problem with if-else shitty statement ?

F:\>javac Example1.java
Example1.java:15: error: '.class' expected
int y=0;
^
Example1.java:15: error: not a statement
int y=0;
^
Example1.java:15: error: illegal start of expression
int y=0;
^
Example1.java:15: error: ';' expected
int y=0;
^
Example1.java:16: error: 'else' without 'if'
else
^
5 errors


Tell the difficulties that i am difficult.
Bharat Kasodariya
Ranch Hand

Joined: Aug 19, 2011
Posts: 36
It should be like

public static void main(String[] args)
{
if(true)
{ int y=0; }
else {
int x=1; }
}
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Thanks for nice advise but it wont affect any.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Related - http://www.coderanch.com/t/472743/java-programmer-SCJP/certification/variable-Declaration
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583


thanks john
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
saloni jhanwar wrote:Thanks for nice advise but it wont affect any.


Did you actually try his suggestion? Because it then compiles cleanly..
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
D. Ogranos wrote:
saloni jhanwar wrote:Thanks for nice advise but it wont affect any.


Did you actually try his suggestion? Because it then compiles cleanly..

I read it like, "even if it works well after applying the braces, I think allowing the option of declaring inside an if without a brace will not affect anything"

Just strange how I did interpret like that
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

D. Ogranos wrote:
saloni jhanwar wrote:Thanks for nice advise but it wont affect any.


Did you actually try his suggestion? Because it then compiles cleanly..


I were looking for reasons not just about successful compilation.
Bharat Kasodariya
Ranch Hand

Joined: Aug 19, 2011
Posts: 36
looking for reason then this can be helpful to you
http://stackoverflow.com/questions/3594427/how-can-you-assign-a-variable-a-value-inside-a-if-statement-in-java
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
saloni jhanwar wrote:
D. Ogranos wrote:
saloni jhanwar wrote:Thanks for nice advise but it wont affect any.


Did you actually try his suggestion? Because it then compiles cleanly..


I were looking for reasons not just about successful compilation.

Other than asking the compiler developers there is no way to know for sure, but my guess would be :
If you don't use braces, then you are effectively declaring a variable that immediately goes out of scope, which is pretty pointless, so the compiler developers decided not to let you do this. Admittedly the error messages could have been a bit more user friendly.
If you do use braces, the compiler developers decided to be a bit more lenient and allow it on the basis that you intend to add code later that makes use of the variable.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
You should be able to get some information from the grammar in the Java™ Language Specification. This is one of the types of “Statement” in the grammar:
if ParExpression Statement [else Statement]
A declaration isn’t a statement, but a block is. I presume SAB is correct that they thought if you put a declaration inside {} you are going to come back and complete the block later.
But also, there are things a compiler can check, and these things become difficult to implement. It takes a lot of effort, and makes compilation slower, to check whether a block contains a statement or not. There are also things a compiler cannot check. It should ensure that whatever is written matches the syntax of the grammar and specification, and no more. You cannot expect a compiler to check for daft code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: if-else statement problem