1

I keep getting an error thrown at the line that I have put a comment above the line that says "unreachable statement." How can I fix this? Is there anything else wrong with this code?

boolean containsAll(IntSet [] s) {


    return false;

    // Unreachable Code begins here
    for (int y = 0; y< s.length; y++) {
        for (int i = 0; i< s[y].arr.length; i++) {
            if (s[y].contains(i)) {
                if (i>=arr.length) {
                    return false;
                }
                if (!arr[i]) {
                    return false;
                }
            }
        }
    }
    return true;
}
Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
gracei
  • 11
  • 2

2 Answers2

3

As @Eli suggested whenever you have return condition before some statements in a function it will never reach that code hence will give you compilation error of

error : unreachable statement

As shown in image below:

Compilation error message

Just remove return false on line 12 as in image above it will compile your program without any error.

Hope it helps!!!

Arshad
  • 351
  • 1
  • 2
  • 15
Anuj Sharma
  • 67
  • 11
1
boolean containsAll(IntSet [] s) {


    return false; // <------- remove this 

    // Unreachable Code begins here
    for (int y = 0; y< s.length; y++) {
        for (int i = 0; i< s[y].arr.length; i++) {
            if (s[y].contains(i)) {
                if (i>=arr.length) {
                    return false;
                }
                if (!arr[i]) {
                    return false;
                }
            }
        }
    }
    return true;
}

It will obviously not reach the code below the return statement. because It will automatically end a method. return is use to pass the Object value to the function/method itself. Try to read this too, it will be a lot of help to understand what is the meaning of return. -->> Returning a Value in Method .

msagala25
  • 1,806
  • 2
  • 17
  • 24