Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with if clause

 
Manish Agarwal
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
when i tried to compile the following code i got an error message saying that the variable y might not have been initialised.I could not understand this since the condition in if will always be true and in that the variable y will be initialised.Can somebody throw some light on this.
thanks in advance.

 
Kris Adams
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler wouldn't evaluate the condition in the if clause. So all it sees is that the initialization is done conditionally and thats the reason for the error.

~SJCP
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish,
the compiler can analyse your code to a certain extent but it cannot predict that the value of x is not going to change before that if statement. It does not "run" your code. If, however, you tell the compiler that x is final then the compiler can do a better analysis and the code compiles cleanly.
 
Murtuza Akhtari
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The compiler wouldn't evaluate the condition in the if clause. So all it sees is that the initialization is done conditionally and thats the reason for the error.

I dont quite agree with Krishna ...
If the code is modified to include an else along with the if then it works just fine !



which means that the compiler checks to see if the variable y is initialized or not...In your case...If x were not 5 thn y would never be initialized. So if u have an else clause, it makes sure tht y is initialized all the time.
Also if u have something like this..

if(true)
y = 0;

then the compiler does not complain..
[ August 10, 2004: Message edited by: Murtuza Akhtari ]
 
Manish Agarwal
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Krishna and Barry.
I hav one more query regarding if and while.When i use while(false) i got an error saying Unreachable statement.But if i use if(false) code compiles fine.Why no error in case of if?Please help me.
thanx.

 
Aarti Dahiya
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Making x final still gives a compilation error.
The only thing that might compile this code is using if(true). The compiler knows that the condition is TRUE always, and does not depend on anything else.
 
Manish Agarwal
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx Murtuza for extra info.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aarti Dahiya:
Making x final still gives a compilation error.


Interestingly I do not get any compilation errors using the 1.5.0 Beta2 compiler. I'll try it out at work tomorrow using the 1.4.2 compiler.
 
Aarti Dahiya
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Barry you are right! Making x final compiles just fine.
I copied the code wrong when I compiled it.
 
Manish Agarwal
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barry, but why the compiler does not complain when we use else along with if ?
 
siva prakash
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,



but for me it is compiling and giving out put also even without declaring x as final . pls help me in getting it




bye
siva
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic