Forums Register Login

What is this unreachable error

+Pie Number of slices to send: Send
Check it.
public class Cjgreen{
public static void main(String argv[]){
Cjgreen c = new Cjgreen();
c.jgreen();

}
public void jgreen(){
int iNum =1 ;
while(iNum >0){

toffer:
for(int i = 0; i < 3; i ++){
continue toffer;
System.out.println(i);
}
}

iNum --;
}
}
above code gives compilation error , says that System.out.println(i); is
unreachable.
than check below code :

public class Hello
{
public static void main(String argv[])
{
final int i=1;
if(i>5)
System.out.println("test");
}

}

above works fine with no errors , but here also System.out.println("test"); is unreachable .

can anybody explain the concept of unreachable correctly
+Pie Number of slices to send: Send
The compiler handles 'if' statements as a 'special case' when determining wether or not a statement is reachable. It does not test the condition. This allows programmers to set up 'flags' that can be used in debugging code.

14.21 Unreachable Statements


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.

 
+Pie Number of slices to send: Send
If you observe this code for any value of i this will not reach system.out.println(i); i.e if you change for loop to some thing like
(for int=0;i<b;i++) where b can be 3 or 100 or 1000 or maximum value of integer but it will still not reach system.out.println(i); statement.

for(int i = 0; i < 3; i ++){
continue toffer;
System.out.println(i);
}


But if you consider the below code:

final int i=1;
if(i>5)
System.out.println("test");
}

if you can change the value of i to some thing greater than six then there is a chance to reach system.out.println statement.

But in above case if you change the value of i to any thing even if for loop is modified to for(; it will not reach print statement. Hence it is giving unreachable error.
+Pie Number of slices to send: Send

if case is unreachable then y case 2 is not unreachable.
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 776 times.
Similar Threads
Iterator expression in for statement.
break statement.
Indefinite loop
Flow control
continue
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:15:57.