1

I have a for loop in my code that's giving an error on the entire loop itself, saying it's "unreachable code". I'm completely unsure about what's causing this with this loop:

public ArrayList<Double> Update(ArrayList<Double> addInputs) 
{
// stores the resultant outputs from each layer

ArrayList<Double> outputs = null;

int cWeight = 0;

// first check that we have the correct amount of inputs

if (addInputs.size() != numInputs);
{
    // just return an empty vector if incorrect
    return outputs;
}

// For each layer....


for (int i = 0; i < numHiddenLayers + 1; ++i)
{
    if (i > 0)
    {
        addInputs = outputs;
    }

    outputs.clear();

    cWeight = 0;

    // for each neuron sum the (inputs * corresponding weights) .Throw

    // the total at our sigmoid function to get the output.

    for (int j = 0; j < neuronLayers.get(i).numNeurons; ++j)
    {
        double netinput = 0;

        int numberInputs = neuronLayers.get(i).listNeurons.get(j).numInputs;

    // for each weight

    for (int k = 0; k < numberInputs - 1; ++k)
    {
        // sum the weights x inputs

        netinput += neuronLayers.get(i).listNeurons.get(j).weights.get(k) *
                addInputs.get(cWeight++);
    }

    // add in the bias

    netinput += neuronLayers.get(i).listNeurons.get(j).weights.get(numberInputs - 1)
            *  dBias;

    // we can store the outputs from each layer as we generate them.

    // the combined activation is first filtered through the sigmoid

    //function

    outputs.add(sigmoid(netinput,dActivationResponse));
    }
}

It's claiming the entire for loop after

// For each layer....

is unreachable code. Any help would be greatly appreciated.

Joseph Oliver
  • 169
  • 1
  • 12

3 Answers3

3

You have a semicolon here, so the method is always returning outputs. Just remove it

if (addInputs.size() != numInputs); // <---- this one
{
    // just return an empty vector if incorrect
    return outputs;
}
Héctor
  • 24,444
  • 35
  • 132
  • 243
  • i'm a complete idiot! I didn't notice it because it flagged up the entire "for" block and i was assuming the error was with that. Fixed that and I had to insert "Return outputs" right at the end as well, so that's two errors fixed. Thanks very much for the help! – Joseph Oliver Nov 16 '17 at 16:00
  • You aren't, we all can be mistaken. Glad to help you! – Héctor Nov 16 '17 at 16:02
1

You have a semicolon at the end of your if:

... != numInputs);

Meaning the return underneath it will always happen, making the code under it unreachable.

Remove the semicolon.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
0

It is saying because if condition terminates because of semicolon

So return will always execute which transfer control to the caller function skipping you rest of the code

Digvijaysinh Gohil
  • 1,367
  • 2
  • 15
  • 30