Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Unreachable statement problem

1. ## Unreachable statement problem

I have been working on the following for hours only to get unreachable code for line 44 return (sum);. Need some fresh eyes asap please.

```package evenFibonaccisum;

import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

public class Sum {
static int sumEvenFib(int start, int end) {
int fn1 = start; //fn_1
int fn2 = getNextTerm(start); //fn_2
int fn = 0; //current term
int sum = fn1 + fn2; //holds sum of even terms
while (fn<=end) {
fn=fn1+fn2;
fn1=fn2;
fn2=fn;
if(fn>=end){
break ;
}
else{

if(fn%2==0){

sum=sum+fn;
}
}
}

int getNextTerm;
int x = 0; {
int a=0;
int b=1;
int fib=1;
for(int i=0; i<=x; i++){
fib=a+b;
a=b;
b=fib;
}
return (fib);
}

return (sum);
}

private static int getNextTerm(int start) {
// TODO Auto-generated method stub
return 0;
}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int start = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*");

int end = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*");

int res = sumEvenFib(start, end);

bufferedWriter.write(String.valueOf(res));
bufferedWriter.newLine();

bufferedWriter.close();

scanner.close();

}
}```

2. ## Re: HELP...I CAN'T FIGURE THIS OUT

Please edit your post and wrap your code with code tags:

[code]
**YOUR CODE GOES HERE**
[/code]

to get highlighting and preserve formatting.

I've done it this time for you.

There is a useless pair of {}s in the code starting at this line:
`int x = 0; {`
Remove that { and its pairing } and you will see two return statements immediately next to each other.
The second return statement is unreachable code

3. ## Re: HELP...I CAN'T FIGURE THIS OUT

Thank you. Removed the {}s. How do I correct for this unreachable code to ensure that I am able to get a result from my input?

4. ## Re: HELP...I CAN'T FIGURE THIS OUT

How do I correct for this unreachable code
Remove one of the return statements so there is only one. For example:
```   return val1;
return val2;    // this statement unreachable```
Removing one of the above return statements so that there is only one return statement will remove the unreachable error.

5. ## Re: Unreachable statement problem

Hello,

here is in Bold the statement that is unreachable. I will intentionally not omit the [code] tags.

static int sumEvenFib(int start, int end) {
int fn1 = start; //fn_1
int fn2 = getNextTerm(start); //fn_2
int fn = 0; //current term
int sum = fn1 + fn2; //holds sum of even terms
while (fn<=end) {
fn=fn1+fn2;
fn1=fn2;
fn2=fn;
if(fn>=end){
break ;
}
else{

if(fn%2==0){

sum=sum+fn;
}
}
}

int getNextTerm;
int x = 0; {
int a=0;
int b=1;
int fib=1;
for(int i=0; i<=x; i++){
fib=a+b;
a=b;
b=fib;
}
return (fib);
}

return (sum);
}

I believe this code needs a little bit of refactoring.

Regards
Alin