• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

why doesn't it compile? while(true) vs if(true)

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A{
int m1(){
while(true)
return 1;
}
}

why does it compiles

class A{
int m1(){
if(true)
return 1;
}
}

and it doesn't?

when i use if(true) it gives me the compiler error: "this method must return a result of type int"

what could be the difference between the constructs while(true) and if(true) ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ravindra rs, please check your private messages. You can see them by clicking My Private Messages.
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Both while and if accepts boolean in their arguments. But here the problem is not concerned with if(true).
As because it is conditional statement that is why the condition may fail.In that case return statement will not be executed.
So the compiler needs a return statement that will at any case be executed.
You can add another return statement after that one.
OR
You should have an else condition which also returns an integer.


Kousik
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because it is not guaranteed that the first statement after being executed while, even as you put true that the compiler does not allow it then as you said a method which needs a kind of return is required that he return anything.
 
ravi satti
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kousik Majumder:
Hi,

As because it is conditional statement that is why the condition may fail.In that case return statement will not be executed.
So the compiler needs a return statement that will at any case be executed.

Kousik



How come the condition if(true) fails. Since true is a compile time constant, the compiler knows that the statements under the condition if(true) will definitely be executed. But why is it still giving the compile time error?

consider the below code

class A{
void m1(){
int x;
if(true)
x = 1;
x++;
}
}

it compiles

class A{
void m1(){
int x;
int y = 1;
if(y == 1)
x = 1;
x++;
}
}

but it doesn't. it gives the compiler error 'the local variable x may not have been initialized'

why? since the condition 'true' in if(true) is compile-time constant. but the value of the condition 'y == 1' is not a compile-time constant.

my doubt is when if(true) avoids the compiler error - 'the local variable x may not have been initialized'
why can't it avoid the error - 'this method must return a result of type int'???
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ravi,

I had the same confusion, here is the thread, where I asked a similar question. Scroll down and you'll see the answer,

previous thread

Check out 8th reply by Fred Rosenberger, that one helped me. It might help you also.

Good Luck
[ October 17, 2007: Message edited by: Khushbu Ghodasara ]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what could be the difference between the constructs while(true) and if(true) ?



There is a difference between while and if in such cases. It has to do with flag variables used by programmers for testing or other similar activities. if statement treats if(constant) assuming that constant can be changed later. The problem is described here.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Ravi,

its does not compile because: compiler is not smart enogh to understand
x++ (as method variable will not automatically initialized), see initially x was not initialized. and compiler does not undersand
value of y so it things "if" block may not pass so x would not have initialized at all. Hence, it gets compiler error. But if you put { after if statement and close } after x++ it will work.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ravi satti:


How come the condition if(true) fails. Since true is a compile time constant, the compiler knows that the statements under the condition if(true) will definitely be executed. But why is it still giving the compile time error?



If you take a look at the relevant part of the JLS, you'll see that this is by design.

allow me to quote:


As an example, the following statement results in a compile-time error:

while (false) { x=3; }

because the statement x=3; is not reachable; but the superficially similar case:

if (false) { x=3; }

does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.

The rationale for this differing treatment is to allow programmers to define "flag variables" such as:

static final boolean DEBUG = false;

and then write code such as:

if (DEBUG) { x=3; }

The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.

 
ravi satti
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brian Cole:
[QB][/QB]



Hi Brian and Nowak,

Thanks for giving me the reference to the JLS about the unusual behaviour of if statement with the constants true and false

Now i understood about the concept of reachability and unreachability issues of if statement

But i have one more little doubt

Consider the below code

int x;
if (true) { x=3; }

Now because of the unusual behaviour of if statement, the compiler can't (should not) decide whether statement x=3 is reachable or not. So it is not sure whether the variable x is initialized to 3 or not.

Suppose if we write the statement 'x++' or some other statment like 'System.out.println(x)' using the variable x, the compiler is not giving the error 'the local variable x may not have been initialized'.

I think the compiler should give this error because the compiler is not sure whether x is initialized or not because of the unusual behaviour of if statement with the constants true and false. ???
 
Water proof donuts! Eat them while reading this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic