• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if-else statement problem

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Bharat Kasodariya
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be like

public static void main(String[] args)
{
if(true)
{ int y=0; }
else {
int x=1; }
}
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for nice advise but it wont affect any.
 
John Jai
Rancher
Posts: 1776
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thanks john
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic